Hi am developing an application. I have set up an ADD button, which if clicked should open the add information form in the same window. I mean should not close and open another window. also after the last text box should show the save button to save the records in database
Here is a swing application that will insert the form data into database. For this, set up an ADD button, which when clicked should open the add information form in the same window. This form data will contain the information entered by the user and saved it to database.
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class Form extends JFrame { JButton ADD,SAVE; JPanel panel; JLabel label1,label2,label3,label4,label5; final JTextField text1,text2,text3,text4,text5; Form(){ setLayout(null); label1 = new JLabel(); label1.setText("UserID:"); text1 = new JTextField(20); label2 = new JLabel(); label2.setText("First Name:"); text2 = new JTextField(20); label3 = new JLabel(); label3.setText("Last Name:"); text3 = new JTextField(20); label4 = new JLabel(); label4.setText("ADDRESS:"); text4 = new JTextField(20); label5 = new JLabel(); label5.setText("Email:"); text5 = new JTextField(20); ADD=new JButton("ADD"); SAVE=new JButton("SAVE"); ADD.setBounds(20,20,100,20); label1.setBounds(20,50,100,20); text1.setBounds(140,50,100,20); label2.setBounds(20,80,100,20); text2.setBounds(140,80,100,20); label3.setBounds(20,110,100,20); text3.setBounds(140,110,100,20); label4.setBounds(20,140,100,20); text4.setBounds(140,140,100,20); label5.setBounds(20,170,100,20); text5.setBounds(140,170,100,20); SAVE.setBounds(140,200,100,20); add(label1); add(text1); add(label2); add(text2); add(label3); add(text3); add(label4); add(text4); add(label5); add(text5); add(ADD); add(SAVE); label1.setVisible(false); text1.setVisible(false); label2.setVisible(false); text2.setVisible(false); label3.setVisible(false); text3.setVisible(false); label4.setVisible(false); text4.setVisible(false); label5.setVisible(false); text5.setVisible(false); SAVE.setVisible(false); setTitle("FORM"); ADD.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ label1.setVisible(true); text1.setVisible(true); label2.setVisible(true); text2.setVisible(true); label3.setVisible(true); text3.setVisible(true); label4.setVisible(true); text4.setVisible(true); label5.setVisible(true); text5.setVisible(true); SAVE.setVisible(true); } }); SAVE.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); String value3=text3.getText(); String value4=text4.getText(); String value5=text5.getText(); try{ Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" ); Statement st=conn.createStatement(); st.executeUpdate("insert into data(id,fname,lname,address,email) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"')"); JOptionPane.showMessageDialog(null,"Inserted Successfully!"); } catch(Exception e){ System.out.print(e); } } }); } } class FormDemo { public static void main(String arg[]) { try { Form frame=new Form(); frame.setSize(300,300); frame.setVisible(true); } catch(Exception e){ } } }
Ads