Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

JSplitPane divider location constant ratio

0 views
Skip to first unread message

Amir Kouchekinia

unread,
Dec 15, 1999, 3:00:00 AM12/15/99
to
JSplitPane has a method to set the divider location to a percentage of the
size of the JSplitPane. I have tried to extend this class with a new
behavior so when the splitpane is resized, the percentage remains constant.
Of course, the percentage still should change when the divider is dragged
around.

I have tried to do this, by adding a component listener to the splitpane,
and manipulating the divider location as the component is resized.
I have not been able to keep the ratio constant. Any ideas?

Thanks.

Amir Kouchekinia

Pascal Dihé

unread,
Dec 16, 1999, 3:00:00 AM12/16/99
to

Amir Kouchekinia wrote:

I had the same problem and it took me over 2 weeks to figure this out. ;o)
This seems to work now:

In the PropertyCangeEvent, you must calculate the new percentage if the
SplitPane ist valid. The percentage will only change, when the Divider was
moved. But this works ONLY if continuousLayout is set to true!

public void propertyChange(PropertyChangeEvent p) {
if (this.isValid()) {
spDividerLocation = (double)this.getDividerLocation() /
(double)this.getWidth();
spDividerLocation = dRound(spDividerLocation, 2);
}
}

Then, when the SplitPane is beeing resized, you can update the DividerLocation
with the new value.

public void componentResized (ComponentEvent c) {
this.setDividerLocation(spDividerLocation);
this.doLayout();
}

private double dround(double value, int pre) {
double expo = Math.pow(10, pre);
double newValue = (Math.round(value* pre)) / (double)pre;
return newValue;
}

Pascal

0 new messages