Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FlowLayout JPanel in BorderLayout.North??  RSS feed

 
scott irwin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to put a panel with flowlayout in the north area of a borderlayout frame. When the frame is resized, I want the North panel to resize to fit any components that flow to the next line.
What I get is components dropping off the end of the North flowlayout to never never land when the boundry is too small. Can this even be done? Is there some update on the borderlayout I should be calling?
 
scott irwin
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm close. I found that if I subclass JPanel and override getPreferredSize() I can change the size of the North. But now I need a way to calculate the required area. getMinimumSize always returns the same size.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try looping through each of the components in the flowlayout panel. Use getY() (or getLocation()) to figure out which components are on the last row and how far down it is, and use getHeight() (or getSize()) on each of the last-row components to figure out to overall height of that row. Add the Y of the last row to the largest height from the last row to get the overall preferred height of the component - maybe add a few pixels for cosmetic purposes.
There are probably other, better ways to do this with a different combination of layout managers, but I'm too tired to think about that now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!