Tuesday, 29 July 2014

Java - A basic ActionListener



//main class
import javax.swing.JFrame;;
public class Main {

    public static void main(String[] args) {
        ActionListnrGui alg =  new ActionListnrGui();
       
        //sets the default close operation for JFrame
        alg.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        //sets the size of JFrame
        alg.setSize(350, 200);
       
        //sets the visibility of JFrame
        alg.setVisible(true);
       

    }

}


/*
   
 */
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ActionListnrGui extends JFrame
{
    private JTextField item1;
    private JTextField item2;
    private JTextField item3;
    private JPasswordField passwordField;
   
    //constructor  initialise items to show in window and adds the action listener to each events
    // the action listener takes object of action handler
    public ActionListnrGui(){
     // this sets the title of JFrame
             super("this is title");
           
             //set the layout
             setLayout(new FlowLayout());
           
             //initialise items and add them  to window
             item1 =  new JTextField(10); add(item1);
             item2 =  new JTextField("blood on the dance floor"); add(item2);
             item3 =  new JTextField("no matter how much u try , u cant edit this",20); item3.setEditable(false) ; add(item3);
           
             passwordField = new JPasswordField("my pass is 123 "); add(passwordField);
           
             //the object of class implementing ActionListener
             theHandler handler =  new theHandler();
           
             //add specified ActionListener object to each field  to recieve action event
             //this finally handles the events
             item1.addActionListener(handler);
             item2.addActionListener(handler);
             item3.addActionListener(handler);
             passwordField.addActionListener(handler);
           
           
           
           
    }

    //inner class that handles listener
    private class theHandler implements ActionListener {
        //called automatically when an event fires aka actionlistener functionality done
        public void actionPerformed(ActionEvent event)
        {
            String string = "";
           
            //find the particular  event fired after press of enter key over text boxes items
            //and save the entered text using event.getActionCommand()
            if(event.getSource() == item1)
                string = String.format("field 1 :  %s" , event.getActionCommand() );
            else if(event.getSource() == item2)
                string = String.format("field 2 :  %s" , event.getActionCommand() );
            else if(event.getSource() == item3)
                string = String.format("field 3 :  %s" , event.getActionCommand() );
            else if ( event.getSource() ==  passwordField )
                string = String.format("passsword field is :  %s" , event.getActionCommand() );
               
                //show the message on screen
                JOptionPane.showMessageDialog(null, string);
        }
       
       
    }   

   
}   
           

No comments:

Post a Comment