Swing - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Form

Swing Form

shape Description

By using Swing Form, developers can build stand alone applications, many developers will use Swing because it consists of more components compared to the AWT. In real time developing Swing Form applications user needs to have database also. For instance to register into the log in page user have to enter his details like user name and password, etc .To store the user data admin needs a database, these type of dynamic application will be use full in restaurants to generate the bill to customer because they can not use more affordable applications like high configure application made up with various technologies needs big database. 

Login Swing Form

shape Example

The following example shows how user can register into the log in form and what is the back work of the log in page after submitting the details of the customer. SwingLoginForm.java [java]package swing;//create a package import javax.swing.*;//import all the required packages import java.awt.*; import java.awt.event.*; import java.sql.*; public class SwingLoginForm extends JFrame implements ActionListener//create a class that should extend JFrame and should implement ActionListener { JLabel l1, l2, l3, l4, l5, l6, l7, l8;//declaring components JTextField tf1, tf2, tf5, tf6, tf7;//required text fields JButton btn1, btn2;//require buttons JPasswordField p1, p2;//require password fields SwingLoginForm()//created a constructor { setVisible(true); setSize(700, 700);//set the size setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//to close the window setTitle("User Login Form in Java");//title of the frame l1 = new JLabel("User Registration Form ");//head of the application l1.setForeground(Color.green);//set the color l1.setFont(new Font("Serif", Font.BOLD, 20));//set the font size l2 = new JLabel("Name:");//create user name field l3 = new JLabel("Email-ID:");//create user email id field l4 = new JLabel("Create Passowrd:");//create user password field l5 = new JLabel("Confirm Password:");//create user confirm password field l6 = new JLabel("Country:");//create uder country l7 = new JLabel("State:");//create user state l8 = new JLabel("Phone No:"); //create user phone number tf1 = new JTextField();//create text field for the user name tf2 = new JTextField(); p1 = new JPasswordField();//create text field for the user password p2 = new JPasswordField(); tf5 = new JTextField(); tf6 = new JTextField(); tf7 = new JTextField(); btn1 = new JButton("Submit");//creating submit button btn2 = new JButton("Clear");//creating the clear button btn1.addActionListener(this);//action listener needs to be added to the button btn2.addActionListener(this);//action listener needs to be added to the button l1.setBounds(100, 30, 400, 30);//set the size to the labels l2.setBounds(80, 70, 200, 30); l3.setBounds(80, 110, 200, 30); l4.setBounds(80, 150, 200, 30); l5.setBounds(80, 190, 200, 30); l6.setBounds(80, 230, 200, 30); l7.setBounds(80, 270, 200, 30); l8.setBounds(80, 310, 200, 30); tf1.setBounds(300, 70, 200, 30);//set the size to the text fields tf2.setBounds(300, 110, 200, 30); p1.setBounds(300, 150, 200, 30);//set the size to the password fields p2.setBounds(300, 190, 200, 30); tf5.setBounds(300, 230, 200, 30); tf6.setBounds(300, 270, 200, 30); tf7.setBounds(300, 310, 200, 30); btn1.setBounds(50, 350, 100, 30);//set the size to the button fields btn2.setBounds(170, 350, 100, 30); add(l1); add(l2); add(tf1); add(l3); add(tf2); add(l4); add(p1); add(l5); add(p2); add(l6); add(tf5); add(l7); add(tf6); add(l8); add(tf7); add(btn1); add(btn2); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btn1) { int x = 0; String s1 = tf1.getText(); String s2 = tf2.getText(); char[] s3 = p1.getPassword(); char[] s4 = p2.getPassword(); String s8 = new String(s3); String s9 = new String(s4); String s5 = tf5.getText(); String s6 = tf6.getText(); String s7 = tf7.getText(); if (s8.equals(s9)) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome"); PreparedStatement ps = con.prepareStatement("insert into reg values(?,?,?,?,?,?)"); ps.setString(1, s1); ps.setString(2, s2); ps.setString(3, s8); ps.setString(4, s5); ps.setString(5, s6); ps.setString(6, s7); ResultSet rs = ps.executeQuery(); x++; if (x > 0) { JOptionPane.showMessageDialog(btn1, "Data Saved Successfully"); } } catch (Exception ex) { System.out.println(ex); } } else { JOptionPane.showMessageDialog(btn1, "Password Does Not Match"); } } else { tf1.setText(""); tf2.setText(""); p1.setText(""); p2.setText(""); tf5.setText(""); tf6.setText(""); tf7.setText(""); } } public static void main(String args[]) { new SwingLoginForm(); } }[/java] Create head message of an application, add the color and font text. [java]l1 = new JLabel("User Registration Form "); l1.setForeground(Color.green); l1.setFont(new Font("Serif", Font.BOLD, 20));[/java] Create Submit button and Clear button and ActionListener to the buttons. [java]btn1 = new JButton("Submit"); btn2 = new JButton("Clear"); btn1.addActionListener(this); btn2.addActionListener(this);[/java] Declare the components of a frame in default constructor. [java]try{ //JDBC CODE }Catch(Exception ex) { System.out.println(ex) }[/java] After entering the details data will be saved, other wise it displays password does not match. [java] if (x > 0) { JOptionPane.showMessageDialog(btn1, "Data Saved Successfully"); } } catch (Exception ex) { System.out.println(ex); } } else { JOptionPane.showMessageDialog(btn1, "Password Does Not Match"); } [/java] Output: After executing the above code, output will be as follows. After entering the details of an user in the form,output will be as follows.

Summary

shape Key Points

  • Swing Form - Developing of static applications and dynamic applications are possible with Swing.
  • Swing Login Form - To develop the real time applications data base is needed in swing also.