Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 725
Maksym Govorischev Avatar asked Dec 22 '22 09:12

Maksym Govorischev


2 Answers

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
like image 193
camickr Avatar answered Dec 28 '22 09:12

camickr


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.

like image 36
Michael Borgwardt Avatar answered Dec 28 '22 10:12

Michael Borgwardt