aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to set component  position in GridBagLayout JPanel ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to set component  position in GridBagLayout JPanel ?" Watch "How to set component  position in GridBagLayout JPanel ?" New topic
Author

How to set component position in GridBagLayout JPanel ?

Kevin Pang
Ranch Hand

Joined: Mar 01, 2005
Posts: 38
How to set component position in GridBagLayout JPanel ? I want to set the components on top left of the panel. now I can only make them on verticle center position of the JPanel.
Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Remember gridx = how far from the left and gridy = how far from the right. You probably want both to be 0 for top left.

You need a constraints object as well, as described in the Java™ Tutorials; you would do well to work out how Cay Horstmann uses constraints; it makes it a lot easier to use.
Chris Nash
Greenhorn

Joined: May 20, 2009
Posts: 28
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.


- Frecklefoot
"What you are good at is not as important as being good at something." --Robert B.Parker
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

Chris Nash wrote:
First off, avoid GridBagLayout at all costs.


Chris,
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.

Kevin,
Just follow the links Campbell has provided, especially Cay Horstmann's link. The will be most helpful.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
In addition to the Java Tutorial, I would like to recommend the following, it was a great help to me.

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Fred Hamilton wrote:In addition to the Java Tutorial, I would like to recommend the following, it was a great help to me.

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
More useful than what I last quoted, but less fun!
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Campbell Ritchie wrote:
Fred Hamilton wrote:In addition to the Java Tutorial, I would like to recommend the following, it was a great help to me.

http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/shortcourse.html
More useful than what I last quoted, but less fun!


I could guess what you might be referring to, there are a couple of possibilities.
Brian Moakley
Greenhorn

Joined: Apr 12, 2007
Posts: 11
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.
John Pisci
Ranch Hand

Joined: Dec 19, 2008
Posts: 44
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.
 
Don't get me started about those stupid light bulbs.
 
subject: How to set component position in GridBagLayout JPanel ?