This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set component position in GridBagLayout JPanel ?

 
Kevin Pang
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47284
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 684
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic