Java Swing Tutorial - Java AbstractButton .addActionListener (ActionListener l)








Syntax

AbstractButton.addActionListener(ActionListener l) has the following syntax.

public void addActionListener(ActionListener l)

Example

In the following code shows how to use AbstractButton.addActionListener(ActionListener l) method.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//from w  ww  .ja va  2  s .  c om
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");
    jb.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        System.out.println(((JComponent) arg0.getSource())
            .getFont());
      }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
  }
}