Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSplitPane splitting 50% precisely

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

like image 640
user278731 Avatar asked Feb 22 '10 14:02

user278731


2 Answers

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);     } } 
like image 97
Adamski Avatar answered Sep 22 '22 17:09

Adamski


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). [...]

like image 30
Peter Lang Avatar answered Sep 21 '22 17:09

Peter Lang