I'm confused how GridBagLayout can be used as if it was GridLayout(0,2).

For example, I would like to lay out components like this:

Label TextFieldForLabel
Label TextFieldForLabel
Label TextFieldForLabel
Label TextFieldForLabel

I thought by making a GridBagConstraints() Object, then setting the gridx and gridy variables, this could be accomplished (Java Sun tutorials). However, I have tried numerous ways of doing this, and I can't get anything laid out correctly. In my code, I tried the following:

-using a pattern of setting gridx to "RELATIVE" and setting gridy to 0, then I'd set gridy to RELATIVE and gridx to 0 for the next component I added.

I also tried a few other ways that didn't work.

Alternatively, are there any layout managers that will make it easy to lay out what I described above, but that will NOT put huge amounts of space inbetween the labels & the textfields? GridLayout puts way too much space between the labels and panels, which is the reason I switched to begin with; the way GridLayout aligns it forces the tab of the JTabbedPane to be larger than it should be (the area that I see on screen is larger than I want it to be).

if it's just ment to create an easy looking gui, NetBeans can do that for you, using a wysiwyg drag&drop environemnt

Eh, no thanks. I want to learn how to do it myself. But I also can't do that because my project is already finished, practically. I don't know how to use netbeans to begin with, so integrating it with an already started project would be foolish at this point. But I do appreciate the advice, I plan to use netbeans once I become more experienced without it.

Have you already read through the entire tutorial on Using the GridBagLayout? The layout you want isn't difficult to achieve, but getting used to the way the constraints work does take some time and experimentation.

For your particular question, try giving the text fields a weightx of 1.0. This will force any extra horizontal space to be allocated to that column.

I figured it out (literally, like two minutes ago, so sorry for not posting about it) - I was using RELATIVE when I should have been explicitly setting gridx and gridy to whatever they needed to be. I'm not saying it would've been impossible to use RELATIVE, but it was easier the other way.

Also, I have extra space between each JLabel and JTextField. So, like I said above it looks like

JLabel toomuchspace JText

you're saying setting weightx to 0 will get rid of some of the extra space? I already read about weightx and weighty (I read the whole tutorial on GridBayLayout), but thats not what I understood from it. But I will try that. Thanks.

Weightx and weighty determine how extra horizontal and vertical space is allocated. It column 0 has a weightx of 0 and column 1 has weightx=1.0, then any extra horizontal space will be given to column 1.

You may need to anchor your components to the west as well, if you want them to remain left-aligned within the container.

Ok, I see. So in my case, if I give each of the JLabels (added in column 1) weightx = 1, they will get that extra space. Also, the layout is working fine right now, so I don't think I will need the anchor. I will resize the window a few times and see what happens and take that into consideration though. Thanks a lot for your help. I'll mark this as solved if I don't have any more questions.


Edit:

setting weightx and weighty aren't having any effect at all on the extra space. The component at the very top is twice as long as the text fields are. Does that have any effect on what is considered extra space?

Yes, if that component is in a single column it will. If you need it to span across multiple columns, set the gridwidth property to that number of columns.

This question has already been answered. Start a new discussion instead.