Action events are fired by subclasses of AbstractButton and includes buttons, checkboxes, and menus. : Actions « Swing JFC « Java






Action events are fired by subclasses of AbstractButton and includes buttons, checkboxes, and menus.

  

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] argv) throws Exception {

    AbstractButton button = new JButton("OK");
    button.addActionListener(new MyActionListener());

    JFrame f = new JFrame();

    f.add(button);
    f.setSize(300, 300);
    f.setVisible(true);

  }
}

class MyActionListener implements ActionListener {
  public void actionPerformed(ActionEvent evt) {

    AbstractButton button = (AbstractButton) evt.getSource();
    System.out.println(button.getName());
  }
}

   
    
  








Related examples in the same category

1.Action DemoAction Demo
2.Creating an Action
3.Enabling an Action
4.UseActions: MenuUseActions: Menu
5.Listing the Actions in a Component
6.Sharing an InputMap or an ActionMap Between Two Components
7.Apply special filter to a JTextField
8.Listing the Key Bindings in a Component
9.A text component has an action map with actions and keymaps with actions
10.extends AbstractAction to create your won actionextends AbstractAction to create your won action