aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Dynamically resize multiple JPanels in BoxLayout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Dynamically resize multiple JPanels in BoxLayout" Watch "Dynamically resize multiple JPanels in BoxLayout" New topic

Dynamically resize multiple JPanels in BoxLayout

Benjamin Schwanke

Joined: Aug 28, 2009
Posts: 6

I am programming a GUI witch a JFrame containing a JScrollPane, which has a JPanel (my mainPanel, BoxLayout.Y_Axis) as its viewPortView.
I can add muliple JPanels to the mainPanel with button clicks. Defined by the BoxLayout these panels align top-down.
Attached a picture of my frame with 3 added panels.

Now my need/problem:
These panels heights should be resizable by mouse (drag/drop) similar to the JSplitPane. But JSplitPane does only support 2 components. Otherwise I needed nested once, which is pretty ugly.

My Approach:
After some research I found this to resize my panels:
But when I use this I need to invoke setpreferredSize(Dimension) explicitly and that makes my BoxLayout fail, when I change my window size. That means, if a resize the main window smaller than preferred size of the panels, my panels do not resize automatically like before.

My Question:
Do you know a better solution or is there even an existing one out there to resize JPanels within a main panel?
Or can you help me out with the automatic "fit-to-windowsize" after invoking setpreferredSize(...)?

I tried a lot, but no approach fullfills my requierements. Sorry for bothering you again.

[Thumbnail for JFrame with multiple panels.jpg]

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2281
Maybe you can override the setPreferredSize() method, instead of using the setPreferredSize() method.

Maybe you need to implement the Scrollable interface on your panels and then override the getScrollableTracksViewportWidth() method to return true.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.

Don't forget to use the Code Formatting Tags so the posted code retains its original formatting. That is done by selecting the code and then clicking on the "Code" button above the question input area.

And you say the problem is when you use setPreferredSize(), so in the SSCCE you don't need to include the custom resizing class. You can add 3 panels to the frame. One will use the setPreferredSize() method. The other two won't. If the problem is the setPreferredSize method then two of the panels should resize correctly, while the third won't.
Benjamin Schwanke

Joined: Aug 28, 2009
Posts: 6
Ok, this is my first SSCCE. I hope it helps to describe my problem.
I added three dummy panels - resizable with a DragBar (inner class).
Again the problem:
If I resize the panel with the drag bar, it keeps its current width even if I reduce the frame's width. I would expect an automatic resize of the panel to the main frame's width. It would be great if you can help me out somehow.
I agree. Here's the link:
subject: Dynamically resize multiple JPanels in BoxLayout