Welcome to Coding Magazine!

Ad 468 X 60

Saturday, January 26, 2013

Event Handling in Java Swing : Implementing Clear , Save and Close Button (Complete Program)

Objective Of The Program  :

This program is all about to implement event handling in java (swing). i have already shared how to create a simple graphical form using java swing components.After creating a form we can implement event handling for different buttons.in this program i have implemented event handling on Clear , Save and Close buttons.

Resource Required :

Before going to implement the following program make sure that you have installed the following softwares in your system
  1. JDK 1.7.0 
  2. Microsoft SQL 2000 server

Program Details :

It is also a well written program like my previous java swing program.
In this program i have used a JDK version JDK1.7.0 and  a server Microsoft SQL 2000 to store data .
I have configured the server as the following for the program :
  1. Date Source Name: LocalServer
  2. User Id : sa
  3. password : (blank)
if you are using other database server you have to configure it as the above.
    Then Create a table containing the fields name  , city , state , mail  as name tblAdd .because one table is required to store data while running the program.

    Program Structure (it contains 4 parts) :

    1. Declaration (Contains all component declarations and Connect() to establish connection with database)
    2. Initialization ( Components Initializations)
    3. Event Handling (implementing Clear , Save , Close events)
    4. main( ) to call and initialize the class object

    Complete Program To Implement Clear ,Save and Close Button In Java (Swing)

    //Source Code By CodingMagazine.blogspot.in

    //program starts

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

     //class starts
     class addFormWin implements ActionListener{
       JFrame f1;
       JLabel lblName,lblCity,lblState,lblMail;
       JTextField txtName,txtCity,txtState,txtMail;
       JButton btnClear,btnClose,btnSave,btnDelete,btnUpdate,btnView;
       JPanel p1,p2;
       GridLayout gl42,gl21;
       FlowLayout fl;
       Connection scon;
       Statement stmtSave;
       String na,ct,st,mid;

     //connect() starts
     public void Connect()
      {
       try
        {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        }
       catch(ClassNotFoundException cnfe)
        {
         System.out.println("Error"+cnfe);
        }
       try
        {
          scon=DriverManager.getConnection("Jdbc:Odbc:LocalServer","sa","");
        }
       catch(SQLException se)
        {
         System.out.println("Error"+se);
        }
      }//Connect() Ends

       //contructor starts
       addFormWin()
        {
          f1=new JFrame("Address Book");

          lblName=new JLabel("Name :");
          lblCity=new JLabel("City :");
          lblState=new JLabel("State :");
          lblMail=new JLabel("E-Mail :");

          txtName=new JTextField(40);
          txtCity=new JTextField(40);
          txtState=new JTextField(40);
          txtMail=new JTextField(40);

          btnClose=new JButton("Close");
          btnSave=new JButton("Save");
          btnDelete=new JButton("Delete");
          btnUpdate=new JButton("Update");
          btnView=new JButton("View");
          btnClear=new JButton("Clear");

          p1=new JPanel();
          p2=new JPanel();

          gl42=new GridLayout(4,2);
          gl21=new GridLayout(2,1);
          fl=new FlowLayout();

          p1.setLayout(gl42);
          p1.add(lblName);
          p1.add(txtName);
          p1.add(lblCity);
          p1.add(txtCity);
          p1.add(lblState);
          p1.add(txtState);
          p1.add(lblMail);
          p1.add(txtMail);

          p2.setLayout(fl);
          p2.add(btnClear);
          p2.add(btnSave);
          p2.add(btnUpdate);
          p2.add(btnDelete);
          p2.add(btnView);
          p2.add(btnClose);

          btnClear.addActionListener(this);
          btnClose.addActionListener(this);
          btnSave.addActionListener(this);

          Connect();

          f1.setLayout(gl21);
          f1.add(p1);
          f1.add(p2);
          f1.setSize(400,200);
          f1.setVisible(true);
          f1.setLocation(350,200);
      
      }//Contructor Ends

     //Event Starts
     public void actionPerformed(ActionEvent ae)
       {
            if(ae.getSource()==btnClear)
             {
               txtName.setText(" ");
               txtCity.setText(" ");
               txtState.setText(" ");
               txtMail.setText(" ");
             }
            else if(ae.getSource()==btnClose)
             {
               f1.setVisible(false);
               f1.dispose();
             }
            else if(ae.getSource()==btnSave)
             {
              na=txtName.getText();
              ct=txtCity.getText();
              st=txtState.getText();
              mid=txtMail.getText();
              try
               {
                stmtSave=scon.createStatement();
                stmtSave.executeUpdate("insert into tblAdd values('"+na+"','"+ct+"','"+st+"','"+mid+"')");
               }
              catch(SQLException se)
               {
                 System.out.println("Error"+se);
               }
             }
       }//Event Ends

    }//class ends

     //main class starts
     class addForm
       {
         public static void main(String[] args)
          {
           addFormWin a1;
           a1=new addFormWin();
          }
       }
     //main class ends
    //program ends

    How To Run The Program :-

    Step 1:-Copy And paste the above program in a notepad (from //program starts to //program ends)
    Step 2:- Save it as name addForm.java (because addForm class contains main( ) )
    Step 2:- And Place it  to c:\program files\java\bin directory (if your JDK is in another drive , then save there but make sure that  you have saved it in \bin folder)
    Step 3:- open command prompt and goto c:\program files\java\bin 
    Compile the program :- 
           javac addForm.java
    Run The Program :-
           java addForm 
    SUCCESS !!   Then You Can see interface like the following .
    Enter data in fields and click save.
    ( Check if Clear and Close buttons are working or not )
    Need  Help ??
    If  You are facing any kind of problem while running the program , then please feel free to ask me by using below comment box .Thanks Also Accepted for successful program execution :)

    SHARE THIS POST   

    • Facebook
    • Twitter
    • Myspace
    • Google Buzz
    • Reddit
    • Stumnleupon
    • Delicious
    • Digg
    • Technorati
    Author: Sarada Prasad
    Sarada Prasad is a creative tech writer and tech enthusias. He loves to share different programming and tech related articles through this blog.Read More →

    0 comments: