Java JSplitPane sanitizeSplitPaneDivider(JSplitPane splitPane)

Here you can find the source of sanitizeSplitPaneDivider(JSplitPane splitPane)

Description

sanitize Split Pane Divider

License

Open Source License

Declaration

public static void sanitizeSplitPaneDivider(JSplitPane splitPane) 

Method Source Code

//package com.java2s;
import java.awt.*;

import javax.swing.*;

public class Main {
    public static void sanitizeSplitPaneDivider(JSplitPane splitPane) {
        // I think that, if JSplitPane can't fit both components' preferred sizes, it persecutes the left/top component,
        // depriving it of any space, even if that means granting the bottom/right component more than its preferred size.
        // This is the, somewhat poorly, documented behavior of the default resizeWeight, which is zero.
        // Getting and setting the divider location doesn't work reliably until long after the UI has been constructed.
        // BasicSplitPaneUI.java suggests this is true until we've been painted.
        Component firstComponent = splitPane.getLeftComponent();
        Component secondComponent = splitPane.getRightComponent();
        double firstLength = getSplitPaneComponentLength(splitPane,
                firstComponent);//from  w w w  . j  av a 2s  .c o  m
        double secondLength = getSplitPaneComponentLength(splitPane,
                secondComponent);
        // If both components were equally sized, we'd want to pass 0.5.
        double preferredWeight = firstLength / (firstLength + secondLength);
        splitPane.setResizeWeight(preferredWeight);
    }

    private static double getSplitPaneComponentLength(JSplitPane splitPane,
            Component component) {
        // The actual size may already have been messed up, so we use the preferred size.
        Dimension size = component.getPreferredSize();
        return (splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT) ? size
                .getWidth() : size.getHeight();
    }
}

Related

  1. getSplitPane(boolean vertical, JComponent left, JComponent right)
  2. getSplitPaneComponentLength(JSplitPane splitPane, Component component)
  3. getSplitPaneSize(JSplitPane splitPane)
  4. hsplit(Component left, Component right, double resizeWeight)
  5. rememberOldHeightRatio (JSplitPane splitPane)
  6. setDividerLocation(final JSplitPane splitter, final int position)
  7. setNewHeightRatio (JSplitPane splitPane, JComponent comp, float oldFraction, int v1, int v2)
  8. setSplitDivider(final JSplitPane split, float proportion)
  9. setSplitPaneDividerColor(final JSplitPane pane, final Color color)