Java JSplitPane setDividerLocation(final JSplitPane splitter, final int position)

Here you can find the source of setDividerLocation(final JSplitPane splitter, final int position)

Description

Force divider location for a JSplitPan with int position.

License

Open Source License

Parameter

Parameter Description
splitter a parameter
position a parameter

Declaration

public static JSplitPane setDividerLocation(final JSplitPane splitter, final int position) 

Method Source Code

//package com.java2s;
/*/*from  w w  w .j  ava2 s .  c  om*/
 *    Geotoolkit - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2013 Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import javax.swing.JSplitPane;

public class Main {
    /**
     * Force divider location for a JSplitPan in percent.
     *
     * @param splitter
     * @param proportion
     * @return
     */
    public static JSplitPane setDividerLocation(final JSplitPane splitter, final double proportion) {
        if (splitter.isShowing()) {
            if (splitter.getWidth() > 0 && splitter.getHeight() > 0) {
                splitter.setDividerLocation(proportion);
            } else {
                splitter.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent ce) {
                        splitter.removeComponentListener(this);
                        setDividerLocation(splitter, proportion);
                    }
                });
            }
        } else {
            splitter.addHierarchyListener(new HierarchyListener() {
                @Override
                public void hierarchyChanged(HierarchyEvent e) {
                    if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && splitter.isShowing()) {
                        splitter.removeHierarchyListener(this);
                        setDividerLocation(splitter, proportion);
                    }
                }
            });
        }
        return splitter;
    }

    /**
     * Force divider location for a JSplitPan with int position.
     *
     * @param splitter
     * @param position
     * @return
     */
    public static JSplitPane setDividerLocation(final JSplitPane splitter, final int position) {
        if (splitter.isShowing()) {
            if (splitter.getWidth() > 0 && splitter.getHeight() > 0) {
                splitter.setDividerLocation(position);
            } else {
                splitter.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent ce) {
                        splitter.removeComponentListener(this);
                        setDividerLocation(splitter, position);
                    }
                });
            }
        } else {
            splitter.addHierarchyListener(new HierarchyListener() {
                @Override
                public void hierarchyChanged(HierarchyEvent e) {
                    if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0 && splitter.isShowing()) {
                        splitter.removeHierarchyListener(this);
                        setDividerLocation(splitter, position);
                    }
                }
            });
        }
        return splitter;
    }
}

Related

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