Why doesn't setDividerLocation() work for my JSplitPane?
Created May 4, 2012
I'm guessing you have the same problem I did. At least I thought it was a problem. If you call this method while you're creating your JSplitPane because naturally, you know at creation time you want your pane to be 25/75 or 40/60 or something you don't see it when it comes up. Well, according to Sun this isn't a bug.
Their response to bug #4276222:
From the javadoc for setDividerLocation(double): This method is implemented in terms of setDividerLocation(int).
This method immediately changes the size of the receiver based on
its current size. If the receiver is not correctly realized and on
screen, this method will have no effect (new dividier location will
become (current size * proportionalLocation) which is 0).
So, as you can see the JSplitPane MUST be visible invoking this method otherwise it will not have the desired effect.
Here's a little bit of a hack to get around the problem:
override these methods in a subclass of JSplitPane and it can store the proportion you
want until it is painted for the first time.
public void setDividerLocation(double proportionalLocation) {
if (!isPainted) {
hasProportionalLocation = true;
this.proportionalLocation = proportionalLocation;
}
else
super.setDividerLocation(proportionalLocation);
}
public void paint(Graphics g) {
if (!isPainted) {
if (hasProportionalLocation)
super.setDividerLocation(proportionalLocation);
isPainted = true;
}
super.paint(g);
}