• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Dynamically resize multiple JPanels in BoxLayout

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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.

JFrame-with-multiple-panels.jpg
[Thumbnail for JFrame-with-multiple-panels.jpg]
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic