aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Setting preferred Size vs occupying the available space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting preferred Size vs occupying the available space" Watch "Setting preferred Size vs occupying the available space" New topic
Author

Setting preferred Size vs occupying the available space

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 601
What my intention is to occupy the rest of the available space
in my subpanels. But when I don't, the JPanels get weird size.
I am using the MigLayout, I try to hog 100% width inside every
JPanel it belongs to. But the outer JPanel has to be set
with a preferred size.

Like this case

the p JPanel and sp JScrollPane have to be preset with a size
In the client machines, we can't predict what the sizes would be in advance
How do I set the size to the maximum width and height of
the space available conveniently without knowing
what the client machine's resolution is.
Thanks
Jack
Bernhard Goetz
Ranch Hand

Joined: Jan 07, 2013
Posts: 42

Unfortunately I don't have a solution for MigLayout at hand. But I'd like to point you to GroupLayout. It allows to create anchors on borders, which may be the thing you need. But it's rather hard to write by hand. I recommend using a gui editor, like the excellent (and free!) Google WindowBuilder for Swing. Which I believe is also a good starting software to get in touch with Swing gui development in general.

Best regards,
Bernhard Goetz
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Control the layout, never the size. This way you never need to worry about the JFrame being resized or screen size or resolution.

e.g.

You can actually skip the BorderLayout.CENTER as it is the default.
The key is to choose a proper layout manager or have a structure with nested layouts as per your requirement

Bernhard Goetz wrote: But I'd like to point you to GroupLayout. It allows to create anchors on borders, which may be the thing you need. But it's rather hard to write by hand.

For a reason. GroupLayouts were designed for DnD type of tools and not hand coding.

Bernhard Goetz wrote: I recommend using a gui editor, like the excellent (and free!) Google WindowBuilder for Swing. Which I believe is also a good starting software to get in touch with Swing gui development in general.

Sigh! I beg to differ. Hand coding is the best. This has been discussed numerous times before. Please search the forums to understand why gui builders are a bad idea.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Setting preferred Size vs occupying the available space