Results 1 to 6 of 6
  1. #1
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Question GridBagLayout and multiple JPanel's on a single JFrame

    Hi!

    I have a form (frame) with a few panels in JTabbedPane on it. Frame has a BorderLayout as tabbed pane will occupy whole form. But each panel should have GridBagLayout as it will contain labels, text fields and text areas.
    Here is the current code I use to set up layouts on all panels

    Java Code:
            // adds a component to a panel
    	private void addComponent(JPanel panel, Component component, int row, int column, int width, int height, GridBagLayout gbl, GridBagConstraints gbc)
    		{
    		gbc.gridx = column;
    		gbc.gridy = row;
    		gbc.gridwidth = width;
    		gbc.gridheight = height;
    		gbl.setConstraints(component, gbc);
    		panel.add(component);		
    		}
    	
    	private void addPanel1Components()
    		{
    		GridBagLayout gbl = new GridBagLayout();
    		GridBagConstraints gbc = new GridBagConstraints();
    		pSoft.setLayout(gbl);
    		
    		addComponent(Panel1, lblName, 0, 0, 1, 2, gbl, gbc);
                    ...
                    // It is followed by other 10+ similar calls for all of the components to be placed on this panel
    		}
    The question is should I create one global (for the current frame that is) GridBagLayout variable and use it with "new GridBagLayout()" operator for all panels or use the current approach?
    As having a procedure with 8 parameters is kind of not good looking code and probably not the optimal one either.

    NOTE: this question is also posted here:
    forums.sun.com/thread.jspa?threadID=5415147

    NOTE: ANSWERED
    Last edited by Cybex; 11-09-2009 at 11:02 AM.

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

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The question is should I create one global (for the current frame that is) GridBagLayout variable and use it with "new GridBagLayout()" operator for all panels or use the current approach?
    As having a procedure with 8 parameters is kind of not good looking code and probably not the optimal one either.

    It' really up to you.
    Some considerations:
    1 if it works and you are satisfied with it, it's okay
    2 if others will have to read/edit your code, will it be easy enough for them to read and understand? Also, how will it look and read if you come back to it in a few months?
    3 if you will want to query and alter any of these layouts in the future you will want a separate instance of GridBagLayout for each container. You can access (and cast) the layout from the container.
    4 you can use a single instance of both GridBagLayout and GridBagConstraints for everything if you like.

  4. #4
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks for advices!

    I I'll stick to single layout for each panel variant. Will try to rewrite the code a bit for it so it would not be too bulky.

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

    Default

    Also seriously look at MigLayout which Google can help you find. It's a powerful and easier alternative to GridBagLayout.

  6. #6
    Cybex is offline Member
    Join Date
    Oct 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks for the info!
    Looks like one more version of a table layout at least after a quick look. Will check it out, cause GridBagLayout is kind of not worth the effort.

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2009, 02:03 PM
  2. Switch JPanels in a single JFrame
    By atom86 in forum AWT / Swing
    Replies: 8
    Last Post: 09-23-2009, 09:30 AM
  3. multiple users with single connection
    By nishi.kishore in forum Java Servlet
    Replies: 3
    Last Post: 09-06-2009, 03:15 PM
  4. Replies: 1
    Last Post: 10-02-2008, 07:03 PM
  5. Replies: 6
    Last Post: 07-28-2008, 06:04 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
  •