views:

264

answers:

3

I have a JSplitPane which when shown should split the pane by 50%.

Now on giving an argument of 0.5 (as suggested) to setDividerLocation, Java seems to treat it as a normal number instead of a percentage. As in, the divider, instead of going to the middle of the pane, is almost at the start of the left pane (the pane is vertically split). Any work arounds?

+1  A: 

The setDividerLocation( double ) method only works on a "realized" frame, which means after you've packed or made the frame visible.

The setDividerLocation( int ) method can be used at any time.

camickr
A: 

If you're happy for the divider to move to the middle every time you resize the pane, you could add a ComponentListener and have its componentResized method call setDividerLocation(0.5).

lins314159
You would use the setResizeWeight(...) method for this.
camickr
+1  A: 

this fixes it:

public class JSplitPane3 extends JSplitPane {
    private boolean hasProportionalLocation = false;
    private double proportionalLocation = 0.5;
    private boolean isPainted = false;

    public void setDividerLocation(double proportionalLocation) {
        if (!isPainted) {
            hasProportionalLocation = true;
            this.proportionalLocation = proportionalLocation;
        } else {
            super.setDividerLocation(proportionalLocation);
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        if (!isPainted) {
            if (hasProportionalLocation) {
                super.setDividerLocation(proportionalLocation);
            }
            isPainted = true;
        }
    }

}
david_p
Just for clarification I have gotten it to work using only paint() and no hasProportionalLocation if I specify it manually in the code. Much shorter than this monstrosity
TheLQ