Example usage for java.awt.event HierarchyEvent getChangeFlags

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

Introduction

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

Prototype

public long getChangeFlags() 

Source Link

Document

Returns a bitmask which indicates the type(s) of HIERARCHY_CHANGED events represented in this event object.

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 w w . j ava2 s .c o  m
        }
    };
    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 w  w .  j  a v  a2 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//from  ww w  .ja  v a2 s .c  o 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 void addWindowListener(final Component source, final WindowListener listener) {
    if (source instanceof Window) {
        ((Window) source).addWindowListener(listener);
    } else {//from  w w w .  j  a  v  a2 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

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  a va2 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  w  w  w .  ja va2s.  c o m*/
            }
        }
    });
}

From source file:Main.java

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

/**
 * Force divider location for a JSplitPan with int position.
 *
 * @param splitter/* w  w  w.  ja v  a  2  s. c  o  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 {//from   w ww  .j  a  v a2 s  .  c  o m
            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());
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopTabSheet.java

protected void detachTab(final int tabIndex) {
    final JComponent tabContent = (JComponent) impl.getComponentAt(tabIndex);
    TabImpl tabAtIndex = null;/*from   ww w  .  j  a  v  a  2s.  com*/
    for (TabImpl tab : tabs) {
        if (DesktopComponentsHelper.getComposition(tab.getComponent()) == tabContent) {
            tabAtIndex = tab;
            if (tab.isLazy() && !tab.isLazyInitialized()) {
                initLazyTab(tabContent);
            }
            break;
        }
    }
    if (tabAtIndex == null) {
        throw new IllegalStateException("Unable to find tab to detach");
    }
    final TabImpl tabToDetach = tabAtIndex;
    final ButtonTabComponent tabComponent = tabToDetach.getButtonTabComponent();
    final JFrame frame = new DetachedFrame(tabComponent.getCaption(), impl);

    frame.setLocationRelativeTo(DesktopComponentsHelper.getTopLevelFrame(this));
    impl.remove(tabContent);
    updateTabsEnabledState();
    frame.setSize(impl.getSize());
    frame.add(tabContent);

    final HierarchyListener listener = new HierarchyListener() {
        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            if ((e.getChangeFlags()
                    & HierarchyEvent.DISPLAYABILITY_CHANGED) == HierarchyEvent.DISPLAYABILITY_CHANGED
                    && !impl.isDisplayable()) {
                attachTab(frame, tabToDetach);
                impl.removeHierarchyListener(this);
            }
        }
    };

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            attachTab(frame, tabToDetach);
            impl.removeHierarchyListener(listener);
        }
    });

    impl.addHierarchyListener(listener);
    frame.setVisible(true);
}