Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java anonymous class that implements ActionListener?

I was recently doing a programming assignment that required us to implement in code a program specified by a UML diagram. At one point, the diagram specified that I had to create an anonymous JButton that displayed a count (starting at one) and decremented each time it was clicked. The JButton and its ActionListener both had to be anonymous.

I came up with the following solution:

public static void main(String[] args) {
  JFrame f = new JFrame("frame");
  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  f.setSize(400, 400);

  f.getContentPane().add(new JButton() {

    public int counter;

    {
      this.counter = 1;
      this.setBackground(Color.ORANGE);
      this.setText(this.counter + "");

      this.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
          counter --;
          setText(counter + "");
        }
      });

    }
  });

  f.setVisible(true);

}

This adds an anonymous JButton, then adds another (inner) anonymous ActionListener to handle events and update the button's text as necessary. Is there a better solution? I'm pretty sure I can't declare an anonymous JButton implements ActionListener (), but is there another more elegant way to achieve the same result?

like image 595
Tim Avatar asked May 21 '09 04:05

Tim


People also ask

Can an anonymous class implement an interface in Java?

A normal class can implement any number of interfaces but the anonymous inner class can implement only one interface at a time. A regular class can extend a class and implement any number of interfaces simultaneously. But anonymous Inner class can extend a class or can implement an interface but not both at a time.

Can anonymous class have multiple methods?

Because the EventHandler<ActionEvent> interface contains only one method, you can use a lambda expression instead of an anonymous class expression. See the section Lambda Expressions for more information. Anonymous classes are ideal for implementing an interface that contains two or more methods.

What event class does an ActionListener respond to in Java?

The Beeper class implements the ActionListener interface, which contains one method: actionPerformed . Since Beeper implements ActionListener , a Beeper object can register as a listener for the action events that buttons fire.


2 Answers

I usually go something like this:

JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("name of button") {
    public void actionPerformed(ActionEvent e) {
        //do stuff here
    }
}));

AbstractAction implements ActionListener so this should satisfy the task.

It can be bad practice to squish so many lines of code together, but if you're used to reading it then it can be quite elegant.

like image 71
Cogsy Avatar answered Oct 11 '22 10:10

Cogsy


It's quite ugly, but you could do the following using the ActionListener method and an anonymous class:

  f.getContentPane().add(new JButton(new AbstractAction("name of button") {
      private int counter = 0;

      public void actionPerformed(ActionEvent e) {
          ((JButton) e.getSource()).setText(Integer.toString(counter--));
      }
  }) {
      {
          setText("1");
      }
  });

To make it easier to access the counter you could move it out to the top level of your class and access it from both places where setText is called.

like image 20
Kothar Avatar answered Oct 11 '22 10:10

Kothar