An anonymous Java class that implements ActionListener?

Recently, I performed a programming assignment, which required us to implement the program specified in the UML diagram in the code. At some point, the diagram indicates that I had to create an anonymous JButton, which showed the score (starting with one) and decreased every time I clicked. JButton and its ActionListener should have been 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, and then adds another (internal) anonymous ActionListener to handle events, and updates the button text if necessary. Is there a better solution? I'm sure I can't declare anonymous JButton implements ActionListener (), but is there another elegant way to achieve the same result?

+5
6

- :

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

AbstractAction ActionListener, .

, , , .

+11

, ActionListener :

  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");
      }
  });

, , setText.

+4

- , , .

JComponent, . /, , - Double Brace - - , , with .

:

JButton button = new JButton();
button.addActionListener(new ActionListener() {
    int counter = 1;
    {
        updateText();
    }
    public void actionPerformed(ActionEvent arg0) {
        --counter;
        updateText();
    }
    private void updateText()
        setText(Integer.toString(counter));
    }
});
f.getContentPane(button);

, , , ( ActionListener JButton) .

, EventQueue.invokeLater, , Swing AWT EDT.

+2

- , , , .

+1

, .

, Core Java/Swing.

SwingBuilder Groovy , , . psuedo:

button(text: '' + counter,
         actionPerformed: {counter--; text = '' + counter + ''},
         constraints:BL.SOUTH)

[http://groovy.codehaus.org/Swing+Builder][1]

, , , , , .

, .

+1

, ;-) :

  • ActionListener Action, .
  • ,
    • ,
    • actionPerformed ( api)
  • (aka: unmaintainable)

, .. , ;-) , Action, ( ) , , ( , , : , IDE

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

        int counter = 1;
        { // constructor block of action
            updateName();
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            counter--;
            updateName();
        }

        private void updateName() {
            putValue(Action.NAME, "" + counter);
        }

    })  { // subclass button 
          {  // constructor block button
            setBackground(Color.PINK);
        }}
    );
0

All Articles