Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Applet button to accept user input

Have Programming Question? Ask it here!
 
 
 


Thembekile
button to accept user input
1 Answer(s)      a year and 11 months ago
Posted in : Applet


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){ public string name,lastname,radiobutton,spinner;

name=JTextfield.getTextfield(); lastname=JTextfield.getTextfield(); show(new Studentinfoview(this)); connectDB(); } i cant figure how i am going to declare the radiobutton and spinner.The user input does not show on my Studentinfo.mdb database it just gives me an error.please help


Advertisement
View Answers

September 18, 2012 at 3:08 PM


Here is java swing example that accepts firstname, lastname and gender from user and insert to database.

import java.sql.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class SwingExample{
JTextField text;

public static void main(String[] args) throws Exception{
SwingExample sf=new SwingExample();
}

public SwingExample(){
JFrame f = new JFrame("Frame in Java Swing");
f.getContentPane().setLayout(null);
JLabel lbl1 = new JLabel("First Name");
final JTextField jt1=new JTextField(15);
JLabel lbl2 = new JLabel("Last Name");
final JPasswordField jt2=new JPasswordField(15);
JLabel lbl3 = new JLabel("Gender");
final JRadioButton Male,Female;
ButtonGroup radioGroup=new ButtonGroup();
Male=new JRadioButton("Male");
radioGroup.add(Male);
Female=new JRadioButton("Female");
radioGroup.add(Female);
JButton button=new JButton("Submit");
text=new JTextField(15);
Male.addActionListener(al);
Female.addActionListener(al);
lbl1.setBounds(50,50,70,30);
lbl2.setBounds(50,90,70,30);
lbl3.setBounds(50,130,70,30);
button.setBounds(50,170,100,30);
jt1.setBounds(110,50,100,30);
jt2.setBounds(110,90,100,30);
Male.setBounds(110,130,100,30);
Female.setBounds(210,130,100,30);
text.setBounds(110,170,100,30);
text.setVisible(false);

button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String fname=jt1.getText();
String lname=jt2.getText();
String g=text.getText();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:access");
PreparedStatement st=con.prepareStatement("insert into data(firstname,lastname,gender) values(?,?,?)");
st.setString(1,fname);
st.setString(2,lname);
st.setString(3,g);
int i=st.executeUpdate();
JOptionPane.showMessageDialog(null,"Data is successfully inserted into database.");
}
catch(Exception ex){}
}
});
f.add(lbl1);
f.add(lbl2);
f.add(lbl3);
f.add(jt1);
f.add(jt2);
f.add(Male);
f.add(Female);
f.add(button);
f.add(text);
f.setSize(500,500);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e) {
text.setText(((JRadioButton) e.getSource()).getText());
}
};
}


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com