Java JSplitPane setSplitDivider(final JSplitPane split, float proportion)

Here you can find the source of setSplitDivider(final JSplitPane split, float proportion)

Description

Moves the supplied JSplitPane divider to the specified proportion.

License

Open Source License

Parameter

Parameter Description
split the <code>JSplitPane</code> whose divider location is to be set.
proportion a double-precision floating point value that specifies a percentage, from zero (top/left) to 1.0 (bottom/right)

Declaration

public static void setSplitDivider(final JSplitPane split, float proportion) 

Method Source Code

//package com.java2s;
/*//  ww w.j  a  va 2 s.  com
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.awt.EventQueue;

import javax.swing.JSplitPane;

public class Main {
    /**
     * Moves the supplied <code>JSplitPane</code> divider to the specified <code>proportion</code>.
     * Valid values for <code>proportion</code> range from <code>0.0F</code>
     * to <code>1.0F</code>.  For example, a <code>proportion</code> of <code>0.3F</code> will move the
     * divider to 30% of the "size" (<i>width</i> for horizontal split, <i>height</i> for vertical split) of the
     * split container that contains the specified <code>Dockable</code>.  If a <code>proportion</code> of less
     * than <code>0.0F</code> is supplied, the value <code>0.0F</code> is used.  If a <code>proportion</code>
     * greater than <code>1.0F</code> is supplied, the value <code>1.0F</code> is used.
     *
     * This method should be effective regardless of whether the split layout in question has been fully realized
     * and is currently visible on the screen.  This should alleviate common problems associated with setting
     * percentages of unrealized <code>Component</code> dimensions, which are initially <code>0x0</code> before
     * the <code>Component</code> has been rendered to the screen.
     *
     * If the specified <code>JSplitPane</code> is <code>null</code>, then this method returns with no action
     * taken.
     *
     * @param split the <code>JSplitPane</code> whose divider location is to be set.
     * @param proportion a double-precision floating point value that specifies a percentage,
     * from zero (top/left) to 1.0 (bottom/right)
     * @see #getSplitPaneSize(JSplitPane)
     * @see JSplitPane#setDividerLocation(double)
     */
    public static void setSplitDivider(final JSplitPane split, float proportion) {
        if (split == null) {
            return;
        }

        proportion = Math.max(0f, proportion);
        final float percent = Math.min(1f, proportion);
        int size = getSplitPaneSize(split);

        if (split.isVisible() && size > 0 && EventQueue.isDispatchThread()) {
            split.setDividerLocation(proportion);
            split.validate();
            return;
        }

        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                setSplitDivider(split, percent);
            }
        });
    }

    public static int getSplitPaneSize(JSplitPane splitPane) {
        if (splitPane == null) {
            return 0;
        }

        boolean horiz = splitPane.getOrientation() == JSplitPane.HORIZONTAL_SPLIT ? true : false;
        return horiz ? splitPane.getWidth() : splitPane.getHeight();
    }
}

Related

  1. hsplit(Component left, Component right, double resizeWeight)
  2. rememberOldHeightRatio (JSplitPane splitPane)
  3. sanitizeSplitPaneDivider(JSplitPane splitPane)
  4. setDividerLocation(final JSplitPane splitter, final int position)
  5. setNewHeightRatio (JSplitPane splitPane, JComponent comp, float oldFraction, int v1, int v2)
  6. setSplitPaneDividerColor(final JSplitPane pane, final Color color)
  7. standardJSplitPane(final JSplitPane splitPane)