• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

How to set component position in GridBagLayout JPanel ?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 78666
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78666
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 78666
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic