JSplitPane splitting 50% precisely
Asked Answered
R

4

27

In Swing, what's the best way to make the JSplitPane to split two jpanels with 50% size each.

It looks like if I don't set preferred sizes on the panels it always makes the first panel almost invisible (2%) and the second one (98%)

Thanks in advance

Rutharuthann answered 22/2, 2010 at 14:27 Comment(0)
A
29

Use

setResizeWeight(.5d);

[...] A value of 0, the default, indicates the right/bottom component gets all the extra space (the left/top component acts fixed), where as a value of 1 specifies the left/top component gets all the extra space (the right/bottom component acts fixed). [...]

Acidimetry answered 22/2, 2010 at 14:35 Comment(1)
setResizeWeight(double) is only used to "distribute extra space when the size of the split pane changes". It does not determine the initial space distribution.Livia
L
29

You should use setDividerLocation(double proportionalLocation) to determine the initial space distribution of the JSplitPane, and then call setResizeWeight(double) with the same value to ensure that the panes are resized in proportion.

Also, be aware: Calling setDividerLocation(double) before the JSplitPane is visible will not work correctly, as the space calculation is based on the Component's current size. Instead you need to involve a nasty hack, such as overriding the JPanel's paint method that contains the JSplitPane:

private boolean painted;

@Override
public void paint(Graphics g) {
    super.paint(g);

    if (!painted) {
        painted = true;
        splitPane.setDividerLocation(0.25);
    }
}
Livia answered 22/2, 2010 at 15:45 Comment(1)
+1 It works great! Only one note to the next guy who fall into this: be aware of the "minimum size" property of the components. If the minimum size is greater than the available space, the first draw of the JSplitPane will work fine, but if you resize the frame, it will be recalculated. In that case, a quick fix: component.setMinimumSize(new Dimensions(0,0))Overripe
P
5

I had a similar problem and I solved it by using a component listener in the parent container and set the divider location on the first resize. Initialise a variable firstResize to true and add this into the parent container constructor:

addComponentListener(new ComponentAdapter(){
    @Override
    public void componentResized(ComponentEvent e) {
        if(firstResize){
            splitPane.setDividerLocation(0.5);
            firstResize = false;
        }
    }
});

This should cause the divider to be centred when the parent container size is first set.

Ptolemy answered 30/9, 2013 at 16:56 Comment(0)
E
1

The solutions here do not take into account the case where the user moves the divider (i.e. a variable divider location). A complete example that takes this into account is available here:

How do you get JSplitPane to keep the same proportional location if the user has moved the location of the divider

Encompass answered 18/3, 2016 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.