As you might guess from the question, Java is not my native language. I am trying to teach myself.
My question is kind of a design question/ how do I do it...
I have two classes, one named MyClient and one named MyGUI. MyClient contains main and does nothing but instantiate my GUI which is of course contained in the MyGUI class.
The GUI will have two buttons on it with an actionListener added to them. I would like to know where the best location for the actionPerformed method should be. Should it be in the MyGUI class or can I have another class, for example DoStuff which implements ActionListener and holds the actionPerformed method?
If I can do the later, how do I get data from the GUI into the new DoStuff class. I have a text area and I need the text in my actionPerformed method.
What's the best practice is use separate classes for design and rest of the processing. Depends on the your design use methods for get and set methods, so in that case you have to think about encapsulation in OO concepts.
This is a simple example that illustrates your question:
MyGUI.java creates a JPanel that can then be placed into a JFrame and displayed. It has a JTextField and two buttons. It has a public method that allows us to add an actionlistener to the buttons in this class and a public method to get the main JPanel out of this class so it can be placed into a JFrame
DoStuff.java is a class that implements ActionListener. Its constructor allows passage of a reference to a MyGUI object so that this actionlistener and the gui can interact
public class MyGUI
public static final String GET_TEXT = "Get Text";
public static final String EXIT = "Exit";
private JPanel mainPanel = new JPanel();
private JTextField textfield = new JTextField(12);
private JButton getTextBtn = new JButton(GET_TEXT);
private JButton showTextBtn = new JButton(EXIT);
public void addActionListener(ActionListener doStuff)
public String getText()
public JComponent getComponent()
Finally, MyClient.java which initiates the classes above, passes a reference from the MyGUI object into the DoStuff object, pass the doStuff actionlistener back into the myGui object, and then creates, sets up and displays the main JFrame
public class DoStuff implements ActionListener
// hold a reference to the MyGUI object here. Later, it would be better
// to use an interface variable and not a class variable here
private MyGUI guiInstance;
private String text = "";
public DoStuff(MyGUI gui)
// initialize the reference in this class's constructor
guiInstance = gui;
public void actionPerformed(ActionEvent e)
String actionCmd = e.getActionCommand();
// call the GUI's public method to get the text
text = guiInstance.getText();
// and display it
System.out.println("Text is: " + text);
else if (actionCmd.equals(MyGUI.EXIT))
// get reference to the button that was pushed
JComponent component = (JComponent)e.getSource();
// from that reference get a reference to the enclosing JFrame
Window window = SwingUtilities.getWindowAncestor(component);
// and dispose it
public class MyClient
private static void createAndShowUI()
// create the GUI object
MyGUI myGui = new MyGUI();
// create the actionlistener obj and pass
// a reference to the GUI object into it.
DoStuff doStuff = new DoStuff(myGui);
// add the actionlistener to the GUI
// create the JFrame and pass the GUI's JPanel into the JFrame's contentPane
JFrame frame = new JFrame("MyGUI");
public static void main(String args)
public void run()
Fubarable - thank you for such a detailed response. You told me exactly what I was needing to know.
I'm glad it helped. Now I can tell you that it isn't the best designed demo because it uses fairly tight coupling between classes.
Much better would be to use interfaces to put a degree of separation between the two main classes.