Kevin Pang wrote:How to set component position in GridBagLayout JPanel ?
First off, avoid GridBagLayout at all costs. You can almost always create the GUI you want using a variety of JPanels using BorderLayout, BoxLayout (x-axis and y-axis) and FlowLayout. And, if positioning components by hand is absolutely necessary, turn off the layout managers altogether (i.e. JPanel.setLayout( null ) ) and position each component manually with setBounds (i.e. component.setBounds( x, y, width, height).
I know this isn't the answer you wanted, but it's the best advice I can give. Even the Swing tutorials say to avoid the GridBagLayout if at all possible.
"What you are good at is not as important as being good at something." --Robert B.Parker
Chris Nash wrote:
First off, avoid GridBagLayout at all costs.
Take it easy. No need to panic.
If someone wants to use/learn to use the Gridbag, comments like these are sure to put them off.
Admittedly Gridbag is a bit difficult to grasp, once you get a hang of it, it is one of the most powerful layouts around.
Just follow the links Campbell has provided, especially Cay Horstmann's link. The will be most helpful.
GridBag is a nightmare's nightmare. I agree it is good to learn the layout managers to make you a better programmer. Just know that GridBag produces a nasty mess of code. Constraints upon constraints. The layout really encourages object bloat. Panels within panels. Mig is really a breath of fresh air. I just rewrote all my gridbag code in mig. There's no going back for me. It's the way a layout manager should work ... ie, working with you instead of against you. Who would have thought that you can layout a gui on one panel without breaking a sweat?
In any case, here's a good swing tutorial to get you started on all the layout managers: Swing Tutorial.
Campbell Ritchie wrote:Maneesh is right, but GridBag is often hard to learn. Google for MigLayout, which I have never used, but is supposed to be much easier to use. Or have a look at the Other GridBag Tutorial
But beware of trying a null Layout. That will give all sorts of problems if the Frame is resized; the lower or right Components may drop off the display altogether.
I can vouch for Miglayout- it is very easy to use and even easier to understand. I think they are trying to get it included in the next java release too.
Despite Miglayout being easy, I am still going to have a go with GridBag because different styles are more suited to different situations with different requirements.
Thanks tiny ad, for helping me escape the terrible comfort of this chair.