I was wondering if there is a way to implement an action listener for a Jbutton without a name. For example, I have the following for loop that creates a button for each letter of the alphabet.
for (int i = 65; i < 91; i++){ alphabetPanel.add(new JButton('<html><center>' + (char)i)); }
Is there a way that I can add an action listener for each of those buttons without getting rid of my for loop and hard coding each JButton and then creating an action listener for each?
Thanks in advance,
Tomek
Your question is a little vague. it would be trivial to modify the loop to add a listener inside the loop:
if you can’t modify the loop you could iterate over all of the panel’s children, and add listeners to any of the children that are jbuttons.
also why are you using html to center the text? isn’t that overkill? doesn’t jbutton already center text?
you could use setHorizontalAlignement(SwingConstants.CENTER) to do it too.