wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Does FlowLayout honor the preferred size of a JPanel? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Does FlowLayout honor the preferred size of a JPanel?" Watch "Does FlowLayout honor the preferred size of a JPanel?" New topic
Author

Does FlowLayout honor the preferred size of a JPanel?

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi All,
Here is the scenario. I have 3 JPanels, with each of them having a preferred size that is different. Here are the sizes:
Main JPanel: 650,280
Left JPanel: 50,280
Middle JPanel: 25,280
Right JPanel: 200,280
I want to add the 3 JPanels to the Main JPanel. I am using the default FlowLayout manager. When I add these JPanels, they all look the exact same size. I thought that FlowLayout manager is supposed to honor the preferred size of components. Is JPanel not considered a component? How can I fix this? Many thanks to you!!!

Barry
Tom Diamond
Ranch Hand

Joined: May 10, 2001
Posts: 98
Nope, FlowLayout does not respect each Component's preffered size. Try BoxLayout instead (http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html). I think it's the only Layout Manager that takes care of preffered size of the Components...
Tom.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Barry,
You are correct, the FlowLayout does honor preferred sizes. You are probably not setting the preferred size. If you are setting the size you are not setting the preferred size, since a JPanel has a default layout manager that will supply a preferred size if one is not given by the UI. The following code shows an example which works as you would expect.

Enjoy,
Manfred.
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

Hi Manfred,
I am setting the preferred size of each JPanel. The only thing that is different between your code and mine is that my main panel (the one that has 3 JPanels in it) is actually a south panel. So, I have a north panel and a south panel (which is the main panel) under that. I also set the preferred size of the south (or main) panel. The north panel also has a preferred size set and uses the GridBagLayout. Can you see anything here that may cause a problem? The north panel does not seem to have any problems. Only the 3 panels in the south panel look exactly the same size, as if it totally ignores the preferred size of each of the 3 panels. Thanks for your help!
Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does FlowLayout honor the preferred size of a JPanel?
 
Similar Threads
JApplet
SpringLayout Question - (code snipet inside)
JButton size
why JTextField gets expanded when i maximize the JFrame window
Layout