简体   繁体   中英

Dynamic resize of Layout Manager areas

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.

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 (eg 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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM