Monday 31 March 2014

ActionListener




This is interface is used for action events. ActionListener interface extends the EventListener interface. It contains only one method actionPerformed.

void actionPerformed(ActionEvent e)
    Method invoked when an action occurs.


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

class ActionListenerEx{
 public static void main(String args[]){
  JFrame myFrame = new JFrame("Event Handling Example");
  
  /* Create a button */
  JButton myButton = new JButton("Click Me");
  
  myFrame.setVisible(true);
  myFrame.setSize(250, 250);
  
  /* Add Button to the frame */
  myFrame.add(myButton);
  
  /* Registers the Listeners */
  myButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    JOptionPane.showMessageDialog(null, "Some Action performed");
   }
  });
 }
}


When you press the “click me” button below information message will be displayed.


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment