Reputation: 31272
I have one actionListener attached to all my Buttons. I have 26 buttons each corresponding to one alphabet. After an alphabet is clicked, I want to disable that button alone. how can I achieve this Jwing? I am pasting a part of code, as my entire is too long and has other details which are not necessary. Thanks
public DetailsPanel(GuessPane guess) {
setLayout(new BorderLayout());
setBorder(BorderFactory.createTitledBorder(" click here "));
JPanel letterPanel = new JPanel(new GridLayout(0, 5));
for (char alphabet = 'A'; alphabet <= 'Z'; alphabet++) {
String buttonText = String.valueOf(alphabet);
JButton letterButton = new JButton(buttonText);
letterButton.addActionListener(clickedbutton(guess));
letterPanel.add(letterButton, BorderLayout.CENTER);
}
add(letterPanel, BorderLayout.CENTER);
}
private ActionListener clickedbutton(final GuessPane guess) {
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton pressedButton = (JButton) e.getSource();
String actionCommand = e.getActionCommand();
try {
System.out.println("actionCommand is: ---" + actionCommand);
guess.setLetter(actionCommand);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Upvotes: 0
Views: 505
Reputation: 7076
You can get the source of the event by using event.getSource()
(and notice getSource()
returns an Object
so you need to cast it aswell):
((AbstractButton)event.getSource()).setEnabled(false);
Upvotes: 1