Results 1 to 9 of 9

Thread: GridBagLayout

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default GridBagLayout

    I've designed a GUI using GridBagLayout and am happy with how it looks, but there is a big problem: if I add any data to either of the JList components, they change size and bugger up the layout... Can I not fix their size and position so that this doesn't happen?

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

    Default

    solution: 1) (my preferred solution) don't use GridBagLayout unless absolutely necessary. Usually a more stable layout can be created by nesting JPanels each with their own layouts.
    2) Study up on GridBagLayout and its rules and regulations. Are you setting the weight value?
    3) If you need specific help, you'll have to post code. No one can even get close to guessing what your problem is based solely on description alone.
    4) Or even post a pic of the proper layout desired and we could give you pointers. (both 3 and 4 together would be even better still!)
    Last edited by Fubarable; 01-24-2009 at 01:36 PM.

  3. #3
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Here's a pic of what it looks like:
    I'm obviously happy to do it with whatever layout manager. What do you recommend for this situation?



    Here's some code:
    Java Code:
    class Panel extends JPanel {
       Panel() {
          setLayout(new GridBagLayout());
          GridBagConstraints c;
    
          JButton myButton = new JButton("HELLO WORLD");
          c = new GridBadConstraints(.......);
          add(newComponent, c);
    
          //create another component
          //new values for c
          add(newComponent, c);
       }
    }
    
    class Driver {
       static void createAndShowUI() {
          JFrame frame = new JFrame("HELLO WORLD");
          frame.getContentPane().add(new Panel());
       }
    }
    I haven't used weight values, because I've set the whole thing to be non-resizable...
    Also, I want the edges of my JList components to be flush with the border of the entire window, i.e. there mustn't be that little gap on the sides...
    Thanks
    Last edited by carderne; 01-24-2009 at 06:45 PM.

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

    Default

    It almost appears that a JTable could work well there. Otherwise, hm, hard to say.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    Judicious use of weightX values should solve your problem.

    Another possibility is that setting an appropriate prototype cell value for the jists (see the API for JList) would "freeze" their widths.

    If neither solves the problem completely, you may need to set a preferred size for each component.

    db

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I use GridBagLayout all the time, but it requires some thought about how to set the constraints to cause the components to set where they should.

    Looking at the concept, I assume you plan to use a JTree on the left and a JTable on the right, with a title area on top and messages below. So, you have three rows and two columns in your main JPanel. The 0 and 2 rows extend across the remaining rows. Use two JPanel's and set their preferred heights.

    In the 1 row, you have two areas, each in its own column. A nice approach is to fill both columns with a JSplitPane. If you do this, your main JPanel is down to one column. The JSPlitPane should take up the entire width and height.

    You JTree and JTable can be larger than the space available, so use JScrollPane in each area of the JSplitPane. Put the JTree and JTable in those.

    You can create only one constraints object, but modify it each time before you use it. You will need to reset the previous settings. Or, you can create a new one each time you add a component. Set the minimum number of constraint properties while achieving the desired result.

    Get your code to that point, and you will be able to ask more specific questions if it doesn't work.

  7. #7
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, I never use gridbags. I use Boxes instead. I find I can lay things out easily using Boxes, and either let them resize, or fix their size also very easily. I've written hundreds of GUIs using nothing but Boxes for layouts.

  8. #8
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Thanks for your pointers...

    Judicious use of weightX values should solve your problem.

    I haven't really looked into the weightx stuff, but I need to figure out exactly how it works and I'll have a fiddle with those.

    Another possibility is that setting an appropriate prototype cell value for the jists (see the API for JList) would "freeze" their widths.

    I'll give that a try.

    If neither solves the problem completely, you may need to set a preferred size for each component.

    That somehow slipped my mind. I'll give that a try too.
    I use GridBagLayout all the time, but it requires some thought about how to set the constraints to cause the components to set where they should.

    Looking at the concept, I assume you plan to use a JTree on the left and a JTable on the right, with a title area on top and messages below. So, you have three rows and two columns in your main JPanel. The 0 and 2 rows extend across the remaining rows. Use two JPanel's and set their preferred heights.

    The pic I posted isn't a concept, it's a screenshot of what my program currently looks like. It's actually a JList on both sides, but that could change and is irrelevant anyway. So should I set up a 3x2 Grid layout with a panel in the left and another in the right? I quite like that idea.

    In the 1 row, you have two areas, each in its own column. A nice approach is to fill both columns with a JSplitPane. If you do this, your main JPanel is down to one column. The JSPlitPane should take up the entire width and height.

    I've never used a JSplitPane, but I'll have a look at that option.

    You JTree and JTable can be larger than the space available, so use JScrollPane in each area of the JSplitPane. Put the JTree and JTable in those.

    You can create only one constraints object, but modify it each time before you use it. You will need to reset the previous settings. Or, you can create a new one each time you add a component. Set the minimum number of constraint properties while achieving the desired result.

    Instead of using the standard of setting each value, (c.gridx = ...) I kept the same 'c' object, but used the complete constructor each time, to be sure that no undesired values were being carried over...

    Get your code to that point, and you will be able to ask more specific questions if it doesn't work.
    Well, I never use gridbags. I use Boxes instead. I find I can lay things out easily using Boxes, and either let them resize, or fix their size also very easily. I've written hundreds of GUIs using nothing but Boxes for layouts.

    I'll have a look at that option.

  9. #9
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    Well I seem to have a decent solution:
    The main Panel uses a vertically arranged BoxLayout,which holds three Panels.
    The top one will house the controls and search bar etc.
    The middle one has the headers and things (Playlists, Title, Artist etc).
    The bottom one has the two JLists side-by-side, in JScrollPanes.

    Then I just added the "0 songs" JLabel at the bottom...

    It is now much better and easier to read.
    Thanks very much for your help.

    One last question, if someone can answer it: how can I remove the little gaps between my components and the edge of the window?

Similar Threads

  1. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 08:54 PM
  2. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM
  3. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 12:16 AM
  4. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 08:03 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
  •