Hi,
some time ago, I've written a static helper method which places the splitter
according to the ratio of the preferred sizes of the contained components.
I'd pretty much like to see this code in SplitPane itself, preferably as
non-static...
Here it is:
/**
* Teilt eine {@link SplitPane} im Verhältnis der enthaltenen Komponenten
* (<code>PreferredWidth</code> bzw. <code>PreferredHeight</code>) auf.
* @param splitPane diese <code>SplitPane</code> wird mittels {@link
SplitPane#setSplitRatio(float)} verändert.
*/
static public void adjustRatioToPreferred(SplitPane splitPane)
{
Component bottomRight = splitPane.getBottomRight();
Component topLeft = splitPane.getTopLeft();
if (bottomRight == null && topLeft == null)
return;
if (bottomRight == null && topLeft != null)
splitPane.setSplitRatio(1.0f);
else if (bottomRight != null && topLeft == null)
splitPane.setSplitRatio(0.0f);
else
{
int topLeftSize;
int bottomRightSize;
switch (splitPane.getOrientation())
{
case HORIZONTAL:
int constraintHeight = splitPane.getHeight() == 0 ? -1 :
splitPane.getHeight();
topLeftSize = topLeft.getPreferredWidth(constraintHeight);
bottomRightSize = bottomRight.getPreferredWidth(constraintHeight);
break;
default: //VERTICAL
int constraintWidth = splitPane.getWidth() == 0 ? -1 :
splitPane.getWidth();
topLeftSize = topLeft.getPreferredHeight(constraintWidth);
bottomRightSize = bottomRight.getPreferredHeight(constraintWidth);
}
float splitRatio = (float)topLeftSize / (topLeftSize + bottomRightSize);
splitPane.setSplitRatio(splitRatio);
}
}