Results 1 to 5 of 5
Like Tree3Likes
  • 1 Post By Fubarable
  • 1 Post By Tolls
  • 1 Post By kjkrum

Thread: How do I structure my GUI? As in separate it into classes etc

  1. #1
    tomtaila is offline Member
    Join Date
    Jul 2011
    Posts
    21
    Rep Power
    0

    Default How do I structure my GUI? As in separate it into classes etc

    Hi. My question is how should I write up my GUI code? Right now I simply have a single GUI class named "GUI" with all my JPanels and other containers in there. Surely this cant be correct. I'm wondering should I have each JPanel in its own class or something like that? Anyways here's my code and a little screen sho of how my gui package is set up:

    Java Code:
    package gui;
    
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.border.Border;
    import javax.swing.border.EtchedBorder;
    import javax.swing.border.MatteBorder;
    
    public class GUI {
    	/** Frame / main window of the application */
    	JFrame jf_frame = new JFrame();
    	/** button 1 */
    	JButton jb_Button1 = new JButton("B1");
    	/** button 2 */
    	JButton jb_Button2 = new JButton("B2");
    	/** button 3 */
    	JButton jb_Button3 = new JButton("B3");
    	/** Main Panel */
    	JPanel jp_mainPanel =  new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main left panel */
    	JPanel jp_mainLeftPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main right panel */
    	JPanel jp_mainRightPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	/** Main top panel */
    	JPanel jp_mainTopPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
    	
    	//DIMENSION CONSTANTS
    	private static final Dimension jf_frameSize = new Dimension(1024, 768);
    	private static final Dimension jp_mainPanelSize = jf_frameSize;
    	private static final Dimension jp_mainTopPanelSize = new Dimension(1024, 118);
    	private static final Dimension jp_mainLeftPanelSize = new Dimension(200, 650);
    	private static final Dimension jp_mainRightPanelSize = new Dimension(824, 650);
    	
    	//COLOR CONSTANTS
    	private static final Color mainTopPanelColor = new Color(224,224,224);
    	private static final Color mainLeftPanelColor = new Color(220,238,238);
    	private static final Color borderShadowColor = new Color(134, 134, 134);
    	
    	//BORDER CONSTANTS
    	private static final Border mainTopPanelBorder = new MatteBorder(0, 0, 1, 0, borderShadowColor);//only has a bottom border
    	private static final Border mainLeftPanelBorder = new MatteBorder(0, 0, 0, 1, borderShadowColor);//only has a right side border
    	
    	
    	
    	
    	/** Starts the GUI application */
    	public static void main(String[] args)
    	{
    		GUI gui = new GUI();
    		gui.start();
    	}
    	
    	
    	
    	/** Sets up the GUI */
    	public void start()
    	{
    		jf_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		jf_frame.setSize(jf_frameSize);
    		jf_frame.setResizable(false);
    		jf_frame.getContentPane().add(jp_mainPanel);
    
    		
    		jp_mainPanel.setMinimumSize(jf_frameSize);
    		jp_mainPanel.setSize(jf_frameSize);
    		jp_mainPanel.setBackground(Color.BLACK);
    		jp_mainPanel.add(jp_mainTopPanel);
    		jp_mainPanel.add(jp_mainLeftPanel);
    		jp_mainPanel.add(jp_mainRightPanel);
    		
    		
    		jp_mainTopPanel.setBackground(mainTopPanelColor);
    		jp_mainTopPanel.setPreferredSize(jp_mainTopPanelSize);//main left panel is one quarter of entire frame width
    		jp_mainTopPanel.setBorder(mainTopPanelBorder);
    		
    			
    		jp_mainLeftPanel.setBackground(mainLeftPanelColor);
    		jp_mainLeftPanel.setPreferredSize(jp_mainLeftPanelSize);//main left panel is one fifth of entire frame width
    		jp_mainLeftPanel.setBorder(mainLeftPanelBorder);
    		jp_mainLeftPanel.add(jb_Button1);
    		
    			
    		jp_mainRightPanel.setBackground(Color.WHITE);
    		jp_mainRightPanel.setPreferredSize(jp_mainRightPanelSize);//main right panel is 4 fifths of entire frame width
    		//jp_mainRightPanel.setBorder(mainTopPanelBorder);
    		jp_mainRightPanel.add(jb_Button2);
    		jp_mainRightPanel.add(jb_Button3);
    		
    		jf_frame.setVisible(true);
    		
    		
    		
    		//Add event listeners///////////
    		
    		jb_Button1.addActionListener(new bButtonListener());
    	}
    	
    	
    	//LISTENER INNER CLASSES//////////////////////
    	
    	class bButtonListener implements ActionListener
    	{
    		@Override
    		/** When button is clicked the text changes */
    		public void actionPerformed(ActionEvent e) {
    			jb_Button1.setText("B1 - clicked");
    		}
    	}
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    }


    How do I structure my GUI? As in separate it into classes etc-screen-shot-2012-09-13-18.15.19.png

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

    Default Re: How do I structure my GUI? As in separate it into classes etc

    I think that your GUI classes should follow the same rules as your non-GUI classes. Those concepts that have a unity of purpose and shared behavior should likely be in one class while those that don't should be separated.
    Tolls likes this.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default Re: How do I structure my GUI? As in separate it into classes etc

    If the JPanels are organisational parts of your JFrame then I wouldn't bother shifting them.
    If they are widgets that may be used elsewhere in your code then separate them out.

    If you have two distinct areas of functionality on your page then you might consider breaking those out.
    I'm thinking a split pane where one side is a list of things, and the other side displays the details of whatever thing is currently selected. I would make those two separate classes since they are functionally distinct (IMO).

    It's a bit "finger in the air".
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: How do I structure my GUI? As in separate it into classes etc

    I'm working on a project right now where I've done something similar. I have a gui package and a public GUI class. But the GUI class is an abstraction; it provides some methods that the rest of the program can use to interact with the GUI. Creating an instance of the GUI class creates instances of other non-public classes that are the actual Swing components.
    Fubarable likes this.
    Get in the habit of using standard Java naming conventions!

  5. #5
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: How do I structure my GUI? As in separate it into classes etc

    Quote Originally Posted by Tolls View Post
    If the JPanels are organisational parts of your JFrame then I wouldn't bother shifting them.
    If they are widgets that may be used elsewhere in your code then separate them out.

    If you have two distinct areas of functionality on your page then you might consider breaking those out.
    I'm thinking a split pane where one side is a list of things, and the other side displays the details of whatever thing is currently selected. I would make those two separate classes since they are functionally distinct (IMO).

    It's a bit "finger in the air".

Similar Threads

  1. Classes in separate files
    By natdizzle in forum New To Java
    Replies: 1
    Last Post: 07-25-2012, 05:35 AM
  2. classes structure
    By stuckonjava in forum New To Java
    Replies: 4
    Last Post: 03-17-2012, 01:07 PM
  3. Event Handling with Separate Classes
    By newbie123 in forum New To Java
    Replies: 9
    Last Post: 05-26-2011, 05:12 AM
  4. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  5. Replies: 1
    Last Post: 11-18-2008, 04:48 PM

Posting Permissions

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