Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help figuring out how to set GridBagConstraints for a specific behavior  RSS feed

 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

Ok, this is actually a better-defined version of a problem I posted somewhat recently.

I'm working on a GUI - the main viewable component is a class that extends JComponent, and that's ALL I'm allowed to assume about it. It does its own rendering, has its own behaviors, etc.

The layout I want to use is really somewhat like BorderLayout in terms of arrangement, and the JComponent will go in the center. HOWEVER, instead of the standard BorderLayout behavior in terms of sizing, filling empty space, etc, the behavior I want is:

CENTER - strictly respects the horizontal and vertical sizes of the JComponent and DOES NOT increase its size in either direction. Always keep it centered in the container.

PAGE_START and PAGE_END - 100% width horizontally. Vertically expands to take up any extra space not taken by CENTER

LINE_START and LINE_END - vertically expands to match CENTER height, and horizontally expands to take up any extra space not used by CENTER.


I figure I can do this with GridBagLayout, but getting the constraints set so I get the behavior I want is driving me nuts! Here's what I've go so far, and what I *think* should work, but it doesn't.

PAGE_START region:
anchor = PAGE_START
fill = BOTH
gridwidth = 3
gridx = 0
gridy = 0
weightx = 1
weighty = 1

PAGE_END region:
anchor = PAGE_END
fill = BOTH
gridwidth = 3
gridx = 0
gridy = 2
weightx = 1
weighty = 1

LINE_START region:
anchor = LINE_START
fill = BOTH
gridwidth = 1
gridx = 0
gridy = 1
weightx = 1
weighty = 1

LINE_END region:
anchor = LINE_END
fill = BOTH
gridwidth = 1
gridx = 2
gridy = 1
weightx = 1
weighty = 1

CENTER region:
anchor = CENTER
fill = NONE
gridwidth = 1
gridx = 1
gridy = 1
weightx = 0
weighty = 0


I can't quite figure out what I'm doing wrong, but so far it's not working.

Thanks in advance to any pointers anyone can give me.
[ April 01, 2008: Message edited by: Joe Vahabzadeh ]
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Vahabzadeh:
The layout I want to use is really somewhat like BorderLayout in terms of arrangement, and the JComponent will go in the center. HOWEVER, instead of the standard BorderLayout behavior in terms of sizing, filling empty space, etc, the behavior I want is:

CENTER - strictly respects the horizontal and vertical sizes of the JComponent and DOES NOT increase its size in either direction. Always keep it centered in the container.

PAGE_START and PAGE_END - 100% width horizontally. Vertically expands to take up any extra space not taken by CENTER

LINE_START and LINE_END - vertically expands to match CENTER height, and horizontally expands to take up any extra space not used by CENTER.


I figure I can do this with GridBagLayout, but getting the constraints set so I get the behavior I want is driving me nuts!


You ought to be able to get GridBagLayout to do this, but it's hard to give any advice since you haven't explained how the results you see deviate from the results you expect.

I feel that I should point out, though, that it may be easier to simply write your own LayoutManager.

Ideally you could just subclass BorderLayout and override layoutContainer(), preferredLayoutSize(), maximumLayoutSize(), and minimumLayoutSize() but that won't work because BorderLayout's fields are package private where (IMHO) they should be protected. But instead you can use the implementation of BorderLayout as a template and make the changes you need.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes, in my typing frenzy I sort of forgot that, didn't I?

Well, what's happening is that, if there's enough room for the entire component in the CENTER section to be shown, it gets shown.

However, if there's NOT enough room for it (ie: the container, in this case a JInternalFrame, is resized smaller than the preferred size of the CENTER component), the component in the CENTER section gets shrunk down to a tiny square (I guess 2 pixels by 2 pixels or so, if I had to estimate).
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Vahabzadeh:
However, if there's NOT enough room for it (ie: the container, in this case a JInternalFrame, is resized smaller than the preferred size of the CENTER component), the component in the CENTER section gets shrunk down to a tiny square (I guess 2 pixels by 2 pixels or so, if I had to estimate).


You still haven't said how exactly you would want it to behave in this
situation, but note that for CENTER did specify weightx = weighty == 0.

If you want to control the behavior exactly, I repeat my recommendation
to write a custom LayoutManager.
 
Joe Vahabzadeh
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, the CENTER should always be it's preferred size, under ALL circumstances.

That said, I figured out that what I did wrong had nothing to do with the layout, etc. It was a switcharound - instead of putting A into B and B into C, I should've been putting A into C and C into B... if this explanation makes any sense....
 
Brian Cole
Author
Ranch Hand
Posts: 949
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Vahabzadeh:
Ah, the CENTER should always be it's preferred size, under ALL circumstances.


You still have to figure out what to do if the Container's size (minus its insets) is smaller than the CENTER's preferred size.

That said, I figured out that what I did wrong had nothing to do with the layout, etc.


I'm glad you solved your problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!