This because you are probably trying to access that button from an anonymous class that you use in this way:
button.addActionListener(
new MyListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//do your things on button }
}
}
);
This doesn't work because in Java anonymous classes cannot see variables declared in methods in which they are declared too since their scope are separated. The only way to let your class see it is forcing the final
constraint which assures the compiler that the variable won't change after being initialized, allowing it to extend its scope to the anonymous classes.
To quickly fix this you can access the button from the ActionEvent
inside the actionPerformed
:
((JButton)e.getSource()).setEnabled(false)
Otherwise you have to concretely declare your ActionListener
somewhere or declare the buttons outside the method with static
or final
attribute.. especially if you plan to modify some elements by an action that is fired by another element.