Results 1 to 5 of 5

Thread: Design question

  1. #1
    kfunk is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default Design question

    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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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
    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    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 MyGUI()
      {
        mainPanel.add(textfield);
        mainPanel.add(getTextBtn);
        mainPanel.add(showTextBtn);
      }
      
      public void addActionListener(ActionListener doStuff)
      {
        getTextBtn.addActionListener(doStuff);
        showTextBtn.addActionListener(doStuff);
      }
      
      public String getText()
      {
        return textfield.getText();
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    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
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    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();
        if (actionCmd.equals(MyGUI.GET_TEXT))
        {
          // 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
          window.dispose();
        }
    
      }
    
    }
    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
    Java Code:
    import javax.swing.JFrame;
    
    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    
        myGui.addActionListener(doStuff);  
        
        
        // create the JFrame and pass the GUI's JPanel into the JFrame's contentPane
        JFrame frame = new JFrame("MyGUI");
        frame.getContentPane().add(myGui.getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  4. #4
    kfunk is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    Fubarable - thank you for such a detailed response. You told me exactly what I was needing to know.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. design question comments wanted - essential class init()
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 07-22-2008, 09:41 PM
  3. Doubt in Design doc
    By Arulraj in forum Forum Lobby
    Replies: 0
    Last Post: 06-10-2008, 11:32 AM
  4. Design Calendar UI
    By praveen.kb in forum AWT / Swing
    Replies: 0
    Last Post: 01-21-2008, 11:54 AM
  5. Design patterns
    By Freddie in forum New To Java
    Replies: 2
    Last Post: 05-12-2007, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •