• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dynamically resize multiple JPanels in BoxLayout

Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: http://groups.google.com/group/de.comp.lang.java/browse_thread/thread/3a23402f8871f10c
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]
Posts: 3204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic