File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does FlowLayout honor the preferred size of a JPanel?

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 523
C++ Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic