views:

625

answers:

4

In which Swing layout manager it is possible to change layout areas programmatically? And how to do this with lowest cost?

I have to create component with functionality similar to JSplitPane but with three panels from scratch. One of the moments is to Expand/Collapse one of the panels after clicking oneTouchExpandable button on the divider. But the problem is that I don't know how to implement this collapse action. I tried just setting panels width to 0, but the layout area which contains this panel doesn't shrink after the component. I tried to do this in all Layout Managers, but effect is the same.

Thanks.

+2  A: 

All layout managers resize dynamically. However, the width and height properties are the result of the layouting, and will be overwritten.

The properties you should look at are preferredSize, minimumSize, and maximumSize - the layout managers base their calculations on those properties, though the exact effect depends on the layout manager (e.g. BorderLayout will give the NORTH, SOUTH, WEST and EAST components their preferred size if possibe and assign the remainder to the CENTER component).

Once you've changed the size properties, you have to call revalidate() on the container, then you should see the changes.

Michael Borgwardt
+1  A: 

When making a change that affects the layout of a panel after the GUI is visible you need to revalidate() the panel which essentially invoke the layout on the panel. In your case it might be easier to simply make the component invisible:

component.setVisible(false);
panel.revalidate();
panel.repaint(); // this is only required sometimes
camickr
+1  A: 

I'm with the revalidate()/preferredSize answers but just wanted to suggest this: don't re-invent the wheel! Use the JideSplitPane (part of JIDE's free "Common Layer") - it supports more than two splits.

CarlG
Yes, I looked on this approach and was ready to use it, but it doesn't allow manipulations with dividers between panels, and I need to simulate JSplitPane behavior with OneTouchExpandable buttons and so on. But for anyone who doesn't need to control dividers it can be a good solution
Maksym Govorischev
Not quite sure what you mean by "it doesn't allaw manipulations..." It acts exactly like the standard split pane, it even has the one touch expandable feature built in...
CarlG
Can you point me to exact place where one touch expandable feature for this approach is explained? Thank you in advance.
Maksym Govorischev
Sure, its in the javadocs. See http://www.jidesoft.com/javadoc/com/jidesoft/swing/JideSplitPane.html#setOneTouchExpandable(boolean)
CarlG
A: 

Thanks to all for the answers. Finally I ended up with combining solutions from several answers.

My final solution is following: I use BorderLayout, set West, Center and East panels and then manipulate their sizes by setting PreferredSize to West and East panels. The scheme of rendering is following: while laying out the components BorderLayout gives East and West panels their PreferredSize and rest of the space to Center panel. So with a bit of easy calculations I can manipulate size of each of three panels painlessly.

I also added dividers(originally just JPanel components with fixed size) to West and East panels(their size is also considered while calculating). For dynamic resize I handle dragging events on this dividers and recalculate panel sizes.

Refreshing is done with following snippet: container.setVisible(false); container.revalidate(); container.repaint(); container.setVisible(true);

I'd like to put this code somewhere to be available for others, but don't know where exactly to do this. So if you know such place, please point me to it in the comments.

Maksym Govorischev