I would like to use factory classes and methods to generate GUI components, but I don't know how and in which class the various listeners should be declared and added to the components.
If I have a simple factory class such as that listed below should I add an ActionListener to the button before it is returned to the calling class. If the answer is "Yes" then how do I add the listener?
class GUIFactory
{
public static JButton getJButton()
{
JButton aButton = new JButton();
return aButton;
}
}
Suppose I wanted to use the getJButton() method to add 5 buttons to the GUI, how would I code the ActionListener so that it would know which button was clicked?
Or should the listeners be added in the calling class?
JFrame gui = new JFrame();
gui.add(AppFactory.getJButton());
I've tried the following
gui.add(GUIFactory.getJButton().addActionListener(new guiButtonListener()));
and got an error:
"void" type not allowed here.