Example usage for java.awt.event HierarchyListener HierarchyListener

List of usage examples for java.awt.event HierarchyListener HierarchyListener

Introduction

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

Prototype

HierarchyListener

Source Link

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  ww w.ja  v a  2s. 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 ww .j a  v a2s  .co 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  w ww  .  ja  v a  2s  .  c om*/
 * @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  ww.  j  av a 2  s .co  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

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  .j a  v  a2s .  c  om*/
            }
        }
    });
}

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.ja  v  a2 s.co m*/
        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/* www.jav  a  2 s .c o  m*/
 * @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.monkeys.gui.PopupWindow.java

public PopupWindow(final Frame parent, final Component base) {
    super(parent);

    if (null == base) {
        throw new NullArgumentException("component");
    }/*from w  w w.  j a  va 2s. c o  m*/

    this.addWindowFocusListener(new WindowAdapter() {
        @Override
        public void windowLostFocus(WindowEvent e) {
            hidePopup();
        }
    });
    base.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            hidePopup();
        }

        @Override
        public void componentMoved(ComponentEvent e) {
            hidePopup();
        }

        @Override
        public void componentHidden(ComponentEvent e) {
            hidePopup();
        }
    });
    base.addHierarchyListener(new HierarchyListener() {
        @Override
        public void hierarchyChanged(HierarchyEvent e) {
            hidePopup();
        }
    });
}

From source file:Main.java

/**
 * Force divider location for a JSplitPan with int position.
 *
 * @param splitter//from w  w w  .  j  a v a2s. 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:pl.otros.logview.gui.LogViewPanelWrapper.java

public LogViewPanelWrapper(String name, Stoppable stoppable, TableColumns[] visibleColumns,
        LogDataTableModel logDataTableModel, DataConfiguration configuration,
        OtrosApplication otrosApplication) {
    this.name = name;
    this.configuration = configuration;
    this.otrosApplication = otrosApplication;
    this.addHierarchyListener(new HierarchyListener() {

        @Override//from w  w w.  j  av  a  2  s. co  m
        public void hierarchyChanged(HierarchyEvent e) {
            if (e.getChangeFlags() == 1 && e.getChanged().getParent() == null) {
                LOGGER.info("Log view panel is removed from view. Clearing data table for GC");
                dataTableModel.clear();
            }
        }
    });
    if (visibleColumns == null) {
        visibleColumns = TableColumns.ALL_WITHOUT_LOG_SOURCE;
    }

    fillDefaultConfiguration();

    stopableReference = new SoftReference<Stoppable>(stoppable);
    // this.statusObserver = statusObserver;
    dataTableModel = logDataTableModel == null ? new LogDataTableModel() : logDataTableModel;
    logViewPanel = new LogViewPanel(dataTableModel, visibleColumns, otrosApplication);

    cardLayout = new CardLayout();
    JPanel panelLoading = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.insets = new Insets(10, 10, 10, 10);
    c.anchor = GridBagConstraints.PAGE_START;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    c.ipadx = 1;
    c.ipady = 1;
    c.weightx = 10;
    c.weighty = 1;

    JLabel label = new JLabel("Loading file " + name);
    panelLoading.add(label, c);
    c.gridy++;
    c.weighty = 3;
    loadingProgressBar = new JProgressBar();
    loadingProgressBar.setIndeterminate(false);
    loadingProgressBar.setStringPainted(true);
    loadingProgressBar.setString("Connecting...");
    panelLoading.add(loadingProgressBar, c);
    statsTable = new JTable();

    c.gridy++;
    c.weighty = 1;
    c.weightx = 2;
    panelLoading.add(statsTable, c);
    c.gridy++;
    c.weightx = 1;
    stopButton = new JButton("Stop, you have imported already enough!");
    stopButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            Stoppable stoppable = stopableReference.get();
            if (stoppable != null) {
                stoppable.stop();
            }
        }
    });
    panelLoading.add(stopButton, c);

    setLayout(cardLayout);
    add(panelLoading, CARD_LAYOUT_LOADING);
    add(logViewPanel, CARD_LAYOUT_CONTENT);
    cardLayout.show(this, CARD_LAYOUT_LOADING);

}