Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

GridBagLayout positioning question  RSS feed

 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, was wondering if someone could give me a quick hand/point out the obvious please?

I'm trying to position 3 JPanels in a 'T' shape, with a toolbar across the top, and two other panels below sharing the horizontal space equally and filling 90% of the vertical space. The lefthand JPanel (formPanel) has a few label and TextField components, and the right has a TextArea.

If I just use empty JPanels there's no problem, the space is shared equally. If I add one custom panel containing multiple components on either side, the side that they are on takes up more than 50% of the horizontal space. However with just the TextArea JPanel on one side or other, the split is 50/50.

I've rewritten the code (and removed both the TextArea and FormPanel for simplicity) to try and demonstrate the problem in as short a space as possible. Let me know if you can't replicate it with just the Toolbar, it should be a cut and paste job.



Toolbar:


I'm sure the answer is simple, but I'm quite new to GridBag. I'm trying to use the oracle docs, but it's pretty poorly described for a beginner, there's a lot of assumed knowledge required. I'm going to stick with it but if anyone knows of a good guide or book on the subject I'd appreciate it! **Edit: Core Java II by Horstmann and Cornell is one to go for?

Regards,

Nick
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:... 3 JPanels in a 'T' shape, with a toolbar across the top, and two other panels below sharing the horizontal space equally and filling 90% of the vertical space.


Put the two 'other' panels in a panel with a GridLayout(1, 2) and add that and the 'toolbar' panel to a BorderLayout.CENTER and NORTH. That won't guarantee the 10% - 90% split though.

GridBagLayout and many others respect the preferredSize of components added to them. Whenever you want components to be equal in size, consider using GridLayout.

Read the API more carefully; weightx and weighty determine how extra space is allocated.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about a box layout for the lower part of the frame, with horizontal alighment and a bit of space in the middle? Only I have forgotten what the space is called. Maybe glue. Read about box layout in the Java® Tutorials.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BoxLaoyut respects preferredSize and maximumSize, so wouldn't fit the bill.

Space can be added using a horizontal/verticalStrut or a rigidArea; horizontal/verticalGlue expands to fill any remaining space and can be useful for e.g. right-aligning a JMenu on a JMenuBar (which has a default BoxLayout).
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys - i probably should've mentioned though that I previously used a borderlayout and it worked fine.

i was trying to replicate the layout in gridbag so that i could practice with that manager!

RE: preferred and maximum sizes, it's why i've put arbitrary borders around the internal components so i can see if any of them are forcing the containing panel to grow. i find it odd that the Toolbar is forcing the first grid to take up proportionately more space, when it only contains two small buttons aligned on the left hand side.

i'm currently doing more reading on the topic, so hopefully will come to an answer!

thanks again

nick
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the code you posted, toolbar has a gridwidth of 1, so doesn't span both columns.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are starting out with GridBag, search for Cai Horstmann's GBC class; it makes GridBag much quicker to use.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!