File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Need help understanding GridBagLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help understanding GridBagLayout" Watch "Need help understanding GridBagLayout" New topic
Author

Need help understanding GridBagLayout

B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

Hi all,

I have a Swing GUI I am creating for my team at work that parses through logs for all sorts of information. One of the features is that they can submit a new error (or solution to an error). This opens up in a separate JFrame that is currently using a GridLayout (I just wanted to get it looking something like what I want in my initial attempt). It looks like this:



All of the JLabels on the left are actually inside of JPanel elements that have a FlowLayout with the FlowLayout.CENTER option set. This prevents them from being placed on the very left side of the frame (with no padding). The four rows with the labels and then JComboBoxes/JTextFields are inside of a GridLayout. This is what I want to change. What I would really like to do is to use a GridBagLayout so that I can have more control over the placement of the items.

I have tried reading this page:

http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html


and I have even looked at the examples in GridBadLayoutDemo.java and ContainerEventDemo.java but it still just isn't clear to me. I want to control the size of each component. I see that I need to specify the first JLabel at gridx=0 and gridy=0, but I don't understand the widthx and widthy stuff, which I believe is where my problem lies. Maybe not.

Basically, how can I specify one component to take up so many pixels, or columns, or some certain percentage of the whole line? For example, if there are 10 columns defined then I want the JLabel to take up the first three, pad some space, and then have the JComboBox take up the next 2 columns. But for the lines with JTextFields, I want the JLabel to take up the first 3 columns, and then have a small padded space, and the text fields take up the remainder of the width (with small padding on the right).

Can someone offer some sample contraints that would help me define something like that?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
It's always worth looking at other tutorials, like this one, ( ), and looking at things like Cai Horstmann's GBC class; if you find his (and Cornell's) book (vol I) there are more details about how to use GBC.

The widthx and widthy fields mean how many columns and how many rows the component occupies. Make sure not to put NONE or similar for fill or anchor, otherwise some of your Components might appear too small.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Also keep in mind that sometime it's much easier just to place panels (with their own layout managers) inside of other panels.
For example, you can create a label panel to go on the left side of your main panel which will contain all the labels. You can then set the labels to display right-justified. The panel then should grow enough to contain the widest label.

You can then create a controls panel to hold all the controls. You can then place both these panels within a main panel which uses GridBagLayout, with the right panel using GridBagConstraints which specify a fill of GridBagConstraints.HORIZONTAL and a weight of 1.0.

Read what Mr. Ritchie suggested and play with it a little and I'm sure it will become clear. If not, come back and show what you have tried at that point so we can see what it's not doing correctly.

Lots of people complain that the GridBagLayout is overly complex, but I really like the level of control it gives you. However, I do have to play with the values sometime to get things to work the way I want (so ... I'm no expert ).

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> All of the JLabels on the left are actually inside of JPanel elements that have a FlowLayout with the FlowLayout.CENTER option set.
> This prevents them from being placed on the very left side of the frame (with no padding).

you are way over-complicating things here
1) FlowLayout.CENTER is the deafult, no need to set it

2) you can set a JLabel's alignment
JLabel label = new JLabel("Hello World",JLabel.CENTER);
so there's no need to use a JPanel (with FlowLayout)

3) if you don't want the label flush left, but lined up, you can add an empty border
JLabel label = new JLabel("Hello World");
label.setBorder(BorderFactory.createEmptyBorder(0,15,0,0));
and they'll all start at the same position, 15 pixels in (or whatever pixels you set)
B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

Cool thank you both for the replies. I will certainly take a look through what you have suggested and see what I can come up with. Usually I can just read the Sun tutorials and figure out what I want to do but this GridBagLayout is the first time I have really been stumped...still just thoroughly confused after reading it multiple times!


Also keep in mind that sometime it's much easier just to place panels (with their own layout managers) inside of other panels.

Totally agree with you there. This is actually what I am doing right now. The overall JFrame is actually using a BorderLayout. I have the "fieldPanel" in BorderLayout.CENTER (which itself is a GridLayout) and then my buttonPanel in BorderLayout.SOUTH. The buttonPanel is a FlowLayout that centers the objects -- hence the 2 JButtons show up exactly as I want them to. Getting the labels and other stuff to show up how I want to in the center panel is going to be the tricky part. ;)

I'll play around your suggestion of using GridBag with only 2 elements and see how that works.
B Mayes
Ranch Hand

Joined: Apr 28, 2010
Posts: 47

Ok...so I have played around with things and definitely understand stuff a lot more. Spliting it up into two panels and then using gridbag only on the right was definitely not working for me. The labels were never aligned vertically with the components on the right, the components were all the same horizontal size (I don't need my first combo box to be as long as the rest of the stuff). I finally realized that I just place all of the labels and other components into my panel. Then I define a GridBagLayout, set the layout of the panel to that grid bag, define a GridBagConstraints object, and then just individually set the behavior for each component by modifying the constraints and then applying them. I just use the insets in the constraints to define borders...so I easily got rid of the JPanels for each JLabel, which simplified things even further.

I am almost there!




Now I would like to determine the easiest way to set alignment of the first combo box (either right or left). Should I embed it into a JPanel and then set the alignment of that JPanel to RIGHT or LEFT?


You have all been a great help so far -- thank you very much. I especially like the flash video. Quite representative of my experience.

Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Yes, of course, using two separate panels (one for the labels and one for the components) was not right - I don't know what I was thinking there - sorry about that.

As for your combo box, just set it's anchor (in the GridBadConstraints used when it is added to the panel). The anchor says where the component will go when there's extra room in the cell where it is placed.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help understanding GridBagLayout