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