I have a JFrame with a BorderLayout and set a JPanel to its center. This panel has a GridLayout with some components.
When I start the frame with a pack()-size, everything is ok. But when the frame’s size gets changed smaller, the whole thing
messes up: all components get disordered. But the best is, after widening the frame, this crap order persists.
I aksed for a solution in another forum, but I only got this (sad, but funny too) adress : http://madbean.com/anim/totallygridbag
I don’t really see what the problem is. Make the Window smaller vertically, and life will be roses.
Just an FYI, Java layout managers all draw components so that the fill the entire area. In this case your grid cells are large, so GridLayout fills the cells with the components. If you don’t want the components to do this (e.g. you want lots of extra space around them) either find a third party layout manager that does what you need, or write your own.
P.S. Frame.pack() will auto-fix the window size for you.
because north and south use up the minimal size while the center one takes up what ever space is left north and center makes sense.
[quote]Version note: Before 1.4, the preferred names for the various areas were different, ranging from points of the compass (for example, BorderLayout.NORTH for the top area) to wordier versions of the constants we use in our examples. The constants our examples use are preferred because they are standard and enable programs to adjust to languages that have different orientations.
[/quote]
oooh poor me use page start and center. 8)
hmm actually looking at it again, the text field at 1,2 suddenly end up at location 2,2 etc.
can you show me the code where you add the components?
TableLayout… it’s what I used instead of gridbag layout. You have excellent control over sizes and resizing. https://tablelayout.dev.java.net/ … the tutorial is quite good and not too big.
Actually, the TableLayout you refer to is different from the one I referenced. I actually used to use that one but switched to the one in JCommon because it’s much easier to use and much more flexible.
I am still a little confused, becaus I reall do not understand why the GridLayout behaves like this.
Huh? Perhaps I don’t understand you suggestion. For testing purposes I added one GridLyout-Panel into the Center of an BorderLayout JFrame(). When I use frame.pack()
it works. But as soon as I modify the size (even if i drag it wider!) it gets disordered. Changing the size, vertically, horizontally, smaller, larger whatever does not restore my
order.
So, wheres the logic? Why does not the order gets the main priority? I think, if the screen gets larger, theres no problem, then all components can be widened too.
If it gets smaller you can shrink all components as far as they all can be displayed. Then you have to cut something off.
Other suggestions to LayoutManagers are nice but for some reasons I need to fix the problem with GridLayout.
It is really annoying how layout managers force the components to fill the available space sometimes. The JCommon TableLayout allows you to disable filling on an individual component level or on the entire table. Also, you may end up having to do something like a FlowLayout inside of a GridLayout if you’re sure you want to use GridLayout.