Reputation: 3
I assume there is a better way than removing every element from the JPanel and remaking them, because that's the workaround I had but now obviously when the button is clicked it's told to remove itself so the action never completes.
Here's my awful code for adding the button action listener:
// Add action listener to increment quest progress when button is pushed
listOfButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
questList[temp].incrementNumerator(1);
refreshButtons();
}
});
Here's my awful code for "refreshing" the JButtons:
// Use when updating data that a JButton displays, destroys & recreates all JButtons on the window
private void refreshButtons() {
for (int i = 0; i < listOfButtons.length; i++) {
// Remove each JButtons from the JFrame
contentPane.remove(listOfButtons[i]);
}
addButtons();
}
Upvotes: 0
Views: 744
Reputation: 51711
You can get hold of the JButton
reference from the ActionEvent
itself.
listOfButtons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
questList[temp].incrementNumerator(1);
((JButton) e.getSource()).setText(questList[temp].getValue());
}
});
I've used questList[temp].getValue()
just as an example. You'll obviously pass what works for your program.
Upvotes: 2