0

Hey all I can change the text of 1 single button easily with "final" but I need to create lots of buttons for a flight booking system, and when the buttons are more, final doesnt work ...

JButton btnBookFlight;

eco = new EconomyClass();
eco.setSeats(5);
for(int i=0;i<20;i++){
    btnBookFlight = new JButton("Book" +i);
    btnBookFlight.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnBookFlight.setBackground(Color.RED);
            btnBookFlight.setOpaque(true);
            btnBookFlight.setText("Clicked");
        }
    });
    btnBookFlight.setBounds(77, 351, 100, 23);
    contentPane.add(btnBookFlight);
} 

I would be glad if you can suggest me any trick to get over this.I want to change a buttons color or text when it is clicked or maybe some other cool effects when mouse over but for now only text or color will be enough =).Thanks for your time!

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Anarkie
  • 657
  • 3
  • 19
  • 46

2 Answers2

4

Use the source of the ActionEvent in the ActionListener

btnBookFlight.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {

      JButton button = (JButton)event.getSource();
      button.setBackground(Color.RED);
      ...
    }
});

btnBookFlight has to be final for the inner class (ActionListener) to access it.

From JLS 8.1.3

Any local variable, formal parameter, or exception parameter used but not declared in an inner class must be declared final.

If this is not permitted, then the JButton may be accessed using the source component of the ActionEvent itself using getSource.

However, that said, the simplest solution would be to move the JButton declaration within the scope of the for loop and make it final:

for (int i = 0; i < 20; i++) {
    final JButton btnBookFlight = new JButton("Book" + i);
    btnBookFlight.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            btnBookFlight.setBackground(Color.RED);
            ...
        }
    });
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • +1 For illustrating how to dynamically find the source of an event. – Code-Apprentice Jun 16 '13 at 21:38
  • I will always use this way from now on but can you also please explain the logic behind? – Anarkie Jun 17 '13 at 14:34
  • 1
    @Anarkie I wonder why you dont move the button within the `for` loop? See update :) – Reimeus Jun 17 '13 at 15:37
  • Hahahaha good point!This also worked makes things much simpler, I am new to swing actually java as well... So I am using helper plugins like Window Builder or modifying the examples I find on the internet to get what I want, and I think when I tried it first in the `for` loop I had some errors and moved it outside... or in the version I had it was outside.But now also works inside :) thanks again!I owe you =) – Anarkie Jun 17 '13 at 16:10
1

Just avoid using anonymous classes for your action listener and the final constraint will disappear.

What I mean is use:

class MyActionListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
    JButton src = (JButton)e.getSource();
    // do what you want

  }
}
Jack
  • 131,802
  • 30
  • 241
  • 343