I have a JSplitPane with two components, A and B, but sometimes I want to be able to hide B, so that either of the following are true:
- components A and B are visible in the JSplitPane
- only component A is visible in the space occupied by the JSplitPane
Is there a way to do this?
Assume HORIZONTAL_SPLIT split with two components (A on left and B on right)
Here is how you would hide A and let B take up all the splitpane's space
To hide component B and show A -
If you have a vertical split, use similar approach and switch
x
withy
andwidth
withheight
For a complete solution, you will have to listen to resize events (if applicable) and recalculate the divider location (which means you will have store the state of what is currently visible somewhere)
If you have references to the component A and B you could user JSplitPane.remove() method
or JComponent.setVisible(false) method of the component A or B.Test code:
I found problems with Hovercraft Full Of Eels' version and made my own.
Hovercraft Full Of Eels' one works, nevertheless graphical bug appears if you click a button, resize the frame, then click again the button ; also like amol said, you may want the splitter position to remain during the process.
Heck, I'll throw in an attempt at a solution...
I use this to toggle a log panel at the bottom of a frame:
This remembers and restores the component size.