• Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout and swing  RSS feed

 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to use the GridBagLayout to arrange things on my JPanel and I was wondering a couple of things.
1) when I add just 1 JLabel it automatically shifts the label to the middle of the screen. What do I need to do to have all elements shift towards the top?
2) I'm adding a JPanel with fields on it to a JFrame. I set the size of the JFrame to 1000 by 1000 but the JPanel was set to 200 by 200 yet it fills in all of the JFrame. How can it changed size and how can I keep the JPanel the size I originally assigned it?
3) I'm adding menu items to a JMenuBar, how can I add some spacing between each menu option on the main menu bar?


Any help would be much appreciated. Thanks.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Have you gone through this tutorial?
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

2. What's the layout of the JFrame (or to be more precise, its contentPane)? The default BorderLayout?

3. One way could be to add a Box.createHorizontalStrut(...) as a spacer ... but why do you want your application to have different menu spacing than that to which the computer user is accustomed?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GridBagLayout can be daunting; many people avoid it. If GridLayout doesn't cut it, have you considered a hierarchy of JPanels using BoxLayout?

Also, there are layouts that are more powerful than GridLayout, yet easier to use than GridBagLayout; GridLayout2 and SGLayout come to mind.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl, the menu seems somewhat cluttered and runs together (in my case) if there isn't some spacing. When multiple words are used for 1 menu item it's hard to see what words belong to what menu options (i.e. Run Setup Maintain Schedule - where there are 2 items but hard to distinguish between what 2). Most cases it looks fine but I'm trying to think ahead in cases I may need some additional spacing.

Ulf, thanks for the suggestions. I'll look into the ones you mentioned a bit more. I could read a textbook on it but I find in many cases what they suggest differs from what many people in forums use so I like to get different perspectives on things such as this.

Appreciate both posts guys. Being new to swing components, having such feedback is quite valuable.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl, to answer your questions .....
1. Have you gone through this tutorial?
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
No. I'll take a look when I get an opportunity. Thanks.

2. What's the layout of the JFrame (or to be more precise, its contentPane)? The default BorderLayout?
Yes, I'm using the default for the JFrame.

3. One way could be to add a Box.createHorizontalStrut(...) as a spacer ... but why do you want your application to have different menu spacing than that to which the computer user is accustomed?
Just to make sure there is enough spacing to make clear what words belong to what menu option. I mentioned an example in the previous post.
 
Rob Hunter
Ranch Hand
Posts: 832
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,
Does GridLayout allow aligning cells up and such (i.e. like HTML tables)? That would be optimum for me, having labels and such line up down the page. When trying to indent elements on the page (not having them touch the side of the JPanel) how do you accomplish this? Setting up some sort of "cellpadding" or inserting some placeholder to fill up space and force things over? I'm trying to get a grasp of what best fits the type of tasks I'm trying to tackle. Like I said I like using HTML tables and the way they are done so whatever comes closest to that would be good.... I am open to alternatives though. :-) Thanks again.

Rob
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the actual cells of a GridLayout are all the same size - they take up all the available room. Their cellspacing can be set (it's called Hgap and Vgap; check the javadocs).

How things are layed out inside of the cells depends on the objects you put there - all objects extending JComponent have horizontal and vertical alignment options as well as padding (called "border" in Swing terms).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. nuff said.

2. I guess you should go through the tutorial for BorderLayout too -- in fact, start at the top
http://download.oracle.com/javase/tutorial/uiswing/layout/index.html
In brief, a component added without a constraint to a container that has a BorderLayout is added at CENTER and fills the area allotted to it. BorderLayout doesn't respect preferredSize when the container is resized, it only takes it into consideration when validating the layout (pack() or revalidate())

3. a) Are you aware of JSeparator? b) more than one word as a top level menu prompt seems like bad design to me, but you know your application and users better.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you come across the GBC class? It makes using GridBag a lot easier; there is more about it in Horstmann's book (vol I).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!