Managing 10 buttons at once

I am trying to manage 10 buttons at once, each labeled 1 to 10. I know I could make 10 inner (or outer) classes to handle the actions, but that seems a little messy to me. If I could find out what the text is of the button that was pressed, I could run this is very little code. However, if I cannot, I need to make 10 different listeners.

What would be the cleanest, easiest, most OOP oriented way to approach this? I'm a java newbie that wants to make sure he is learning the correct way.

[504 byte] By [Nathan__a] at [2007-11-26 17:51:46]
# 1

Untested:

final ActionListener actionListener = new ActionListener() {

public void actionPerformed(final ActionEvent ae) {

final JButton button = (JButton) ae.getSource();

System.out.println(button.getActionCommand());

}

};

final JButton[] myButtons = new JButton[10];

for (int i = 0; i < 10; i++) {

myButtons[i] = new JButton(Integer.toString(i));

myButtons[i].addActionListener(listener);

}

es5f2000a at 2007-7-9 5:04:17 > top of Java-index,Security,Event Handling...
# 2
That's exactly what I wanted -- the getSource() function. Thanks a ton.
Nathan__a at 2007-7-9 5:04:17 > top of Java-index,Security,Event Handling...