Example usage for java.awt.event HierarchyEvent SHOWING_CHANGED

List of usage examples for java.awt.event HierarchyEvent SHOWING_CHANGED

Introduction

In this page you can find the example usage for java.awt.event HierarchyEvent SHOWING_CHANGED.

Prototype

int SHOWING_CHANGED

To view the source code for java.awt.event HierarchyEvent SHOWING_CHANGED.

Click Source Link

Document

A change flag indicates that the HIERARCHY_CHANGED event was generated due to the changing of the hierarchy showing state.

Usage

From source file:JSplitPaneVerticalSetTopBottom.java

public static void main(String[] a) {
    JFrame horizontalFrame = new JFrame();
    horizontalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComponent topButton = new JButton("Left");
    JComponent bottomButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);

    HierarchyListener hierarchyListener = new HierarchyListener() {
        public void hierarchyChanged(HierarchyEvent e) {
            long flags = e.getChangeFlags();
            System.out.println(e.getSource());
            if ((flags & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                splitPane.setDividerLocation(.75);
            }/*from  w  ww  .j av  a  2s .  c om*/
        }
    };
    splitPane.addHierarchyListener(hierarchyListener);

    splitPane.setTopComponent(topButton);
    splitPane.setBottomComponent(bottomButton);

    horizontalFrame.add(splitPane, BorderLayout.CENTER);
    horizontalFrame.setSize(150, 150);
    horizontalFrame.setVisible(true);

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    JFrame vFrame = new JFrame("Vertical Split");
    vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent leftButton = new JButton("Left");
    JComponent rightButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setOneTouchExpandable(true);
    splitPane.setLeftComponent(leftButton);
    splitPane.setRightComponent(rightButton);

    ActionListener oneActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.resetToPreferredSizes();
        }//from w  ww  .  ja va2 s.c  o m
    };
    ((JButton) rightButton).addActionListener(oneActionListener);

    ActionListener anotherActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.setDividerLocation(10);
            splitPane.setContinuousLayout(true);
        }
    };

    ((JButton) leftButton).addActionListener(anotherActionListener);

    HierarchyListener hierarchyListener = new HierarchyListener() {
        public void hierarchyChanged(HierarchyEvent e) {
            long flags = e.getChangeFlags();
            if ((flags & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                splitPane.setDividerLocation(.75);
            }
        }
    };
    splitPane.addHierarchyListener(hierarchyListener);

    vFrame.add(splitPane, BorderLayout.CENTER);
    vFrame.setSize(300, 150);
    vFrame.setVisible(true);
}

From source file:Main.java

/**
 * Adds a listener to the window parent of the given component. Can be
 * before the component is really added to its hierachy.
 * @param source The source component/*w  w  w.j a v  a  2s .co m*/
 * @param listener The listener to add to the window
 */
public static void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {
        source.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                    SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
                }
            }
        });
    }
}

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 {//from   w  w w.j  av  a2  s. c  om
            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

public static void onShown(final JComponent component, final Runnable action) {
    component.addHierarchyListener(new HierarchyListener() {
        public void hierarchyChanged(final HierarchyEvent e) {
            if (e.getComponent() == component && (e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) > 0) {
                if (component.isShowing())
                    action.run();//from ww  w  .j  a  va 2s .  com
            }
        }
    });
}

From source file:Main.java

public static void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {//  ww w  . ja va2  s.c  om
        source.addHierarchyListener(new HierarchyListener() {
            @Override
            public void hierarchyChanged(HierarchyEvent e) {
                if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                    SwingUtilities.getWindowAncestor(source).addWindowListener(listener);
                }
            }
        });
    }
}

From source file:Main.java

/**
 * Force divider location for a JSplitPan in percent.
 *
 * @param splitter//from  w w  w .j  a  v  a2 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:Main.java

/**
 * Force divider location for a JSplitPan with int position.
 *
 * @param splitter// www  . j a v  a 2 s.co  m
 * @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

@Override
public void hierarchyChanged(HierarchyEvent e) {
    System.out.println("Components Change: " + e.getChanged());
    if ((e.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0) {
        if (e.getComponent().isDisplayable()) {
            System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
        } else {//  ww w.  jav  a  2s .  com
            System.out.println("Components DISPLAYABILITY_CHANGED : " + e.getChanged());
        }
    }
    if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
        if (e.getComponent().isDisplayable()) {
            System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
        } else {
            System.out.println("Components SHOWING_CHANGED : " + e.getChanged());
        }
    }
}