Example usage for javax.swing JSplitPane addComponentListener

List of usage examples for javax.swing JSplitPane addComponentListener

Introduction

In this page you can find the example usage for javax.swing JSplitPane addComponentListener.

Prototype

public synchronized void addComponentListener(ComponentListener l) 

Source Link

Document

Adds the specified component listener to receive component events from this component.

Usage

From source file:Main.java

public static JSplitPane setDividerLocation(final JSplitPane splitter, final double proportion) {
    if (splitter.isShowing()) {
        if (splitter.getWidth() > 0 && splitter.getHeight() > 0) {
            splitter.setDividerLocation(proportion);
        } else {//  w  w w .j  a  va 2 s  .  c o  m
            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;
}

From source file:Main.java

/**
 * Force divider location for a JSplitPan with int position.
 *
 * @param splitter/* www. ja va 2 s  .  c om*/
 * @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;
}

From source file:Main.java

/**
 * Force divider location for a JSplitPan in percent.
 *
 * @param splitter// w w  w  . j a v  a 2 s. c  om
 * @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;
}

From source file:org.pentaho.ui.xul.swing.SwingElement.java

public void layout() {
    super.layout();
    double totalFlex = 0.0;

    if (isVisible() == false) {
        resetContainer();/*from w w  w. j av a 2s. c o  m*/
        return;
    }

    for (Element comp : getChildNodes()) {
        // if (comp.getManagedObject() == null) {
        // continue;
        // }
        if (((XulComponent) comp).getFlex() > 0) {
            flexLayout = true;
            totalFlex += ((XulComponent) comp).getFlex();
        }
    }

    double currentFlexTotal = 0.0;

    Align alignment = (getAlign() != null) ? Align.valueOf(this.getAlign().toUpperCase()) : null;

    for (int i = 0; i < getChildNodes().size(); i++) {
        XulComponent comp = (XulComponent) getChildNodes().get(i);
        gc.fill = GridBagConstraints.BOTH;

        if (comp instanceof XulSplitter) {
            JPanel prevContainer = container;
            container = new ScrollablePanel(new GridBagLayout());
            container.setOpaque(false);

            final JSplitPane splitter = new JSplitPane(
                    (this.getOrientation() == Orient.VERTICAL) ? JSplitPane.VERTICAL_SPLIT
                            : JSplitPane.HORIZONTAL_SPLIT,
                    prevContainer, container);
            splitter.setContinuousLayout(true);

            final double splitterSize = currentFlexTotal / totalFlex;
            splitter.setResizeWeight(splitterSize);
            if (totalFlex > 0) {
                splitter.addComponentListener(new ComponentListener() {
                    public void componentHidden(ComponentEvent arg0) {
                    }

                    public void componentMoved(ComponentEvent arg0) {
                    }

                    public void componentShown(ComponentEvent arg0) {
                    }

                    public void componentResized(ComponentEvent arg0) {
                        splitter.setDividerLocation(splitterSize);
                        splitter.removeComponentListener(this);
                    }

                });

            }

            if (!flexLayout) {
                if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
                    gc.weighty = 1.0;
                } else {
                    gc.weightx = 1.0;
                }

                prevContainer.add(Box.createGlue(), gc);
            }
            setManagedObject(splitter);
        }

        Object maybeComponent = comp.getManagedObject();
        if (maybeComponent == null || !(maybeComponent instanceof Component)) {
            continue;
        }
        if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
            gc.gridheight = comp.getFlex() + 1;
            gc.gridwidth = GridBagConstraints.REMAINDER;
            gc.weighty = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex);
        } else {
            gc.gridwidth = comp.getFlex() + 1;
            gc.gridheight = GridBagConstraints.REMAINDER;
            gc.weightx = (totalFlex == 0) ? 0 : (comp.getFlex() / totalFlex);
        }

        currentFlexTotal += comp.getFlex();

        if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
            if (alignment != null) {
                gc.fill = GridBagConstraints.NONE;
                switch (alignment) {
                case START:
                    gc.anchor = GridBagConstraints.WEST;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.CENTER;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.EAST;
                    break;
                }
            }

        } else {
            if (alignment != null) {
                gc.fill = GridBagConstraints.NONE;
                switch (alignment) {
                case START:
                    gc.anchor = GridBagConstraints.NORTH;
                    break;
                case CENTER:
                    gc.anchor = GridBagConstraints.CENTER;
                    break;
                case END:
                    gc.anchor = GridBagConstraints.SOUTH;
                    break;
                }
            }
        }

        Component component = (Component) maybeComponent;

        if (comp.getWidth() > 0 || comp.getHeight() > 0) {
            Dimension minSize = component.getMinimumSize();
            Dimension prefSize = component.getPreferredSize();

            if (comp.getWidth() > 0) {
                minSize.width = comp.getWidth();
                prefSize.width = comp.getWidth();
            }
            if (comp.getHeight() > 0) {
                minSize.height = comp.getHeight();
                prefSize.height = comp.getHeight();
            }
            component.setMinimumSize(minSize);
            component.setPreferredSize(prefSize);
        }

        container.add(component, gc);

        if (i + 1 == getChildNodes().size() && !flexLayout) {
            if (this.getOrientation() == Orient.VERTICAL) { // VBox and such
                gc.weighty = 1.0;

            } else {
                gc.weightx = 1.0;

            }
            container.add(Box.createGlue(), gc);
        }
    }

}