Example usage for javax.swing JComponent addAncestorListener

List of usage examples for javax.swing JComponent addAncestorListener

Introduction

In this page you can find the example usage for javax.swing JComponent addAncestorListener.

Prototype

public void addAncestorListener(AncestorListener listener) 

Source Link

Document

Registers listener so that it will receive AncestorEvents when it or any of its ancestors move or are made visible or invisible.

Usage

From source file:Main.java

public static void requestFocusOnDisplay(JComponent c) {
    final JComponent c2 = c;

    c2.addAncestorListener(new javax.swing.event.AncestorListener() {
        public void ancestorAdded(javax.swing.event.AncestorEvent event) {
            c2.requestFocus();// www.  j a  v a 2 s . c o  m
            c2.removeAncestorListener(this);
        }

        public void ancestorRemoved(javax.swing.event.AncestorEvent event) {
        }

        public void ancestorMoved(javax.swing.event.AncestorEvent event) {
        }
    });
}

From source file:Main.java

public static void focusOnOpen(final JComponent component) {
    /*/*ww  w .  j a va  2s.co  m*/
    final Window window = SwingUtilities.getWindowAncestor(component);
    if (window != null)
      window.addWindowListener(new WindowAdapter() {
    public void windowOpened(WindowEvent e) {
      component.requestFocus();
      window.removeWindowListener(this);
    }
      });
     */

    component.addAncestorListener(new AncestorListener() {
        public void ancestorAdded(AncestorEvent event) {
            component.requestFocusInWindow();
            component.removeAncestorListener(this);
        }

        public void ancestorRemoved(AncestorEvent event) {
        }

        public void ancestorMoved(AncestorEvent event) {
        }
    });
}

From source file:Main.java

private static void addJContainerListeners(JComponent c, Object... objs) {
    if (c == null)
        return;/*from   ww  w  .j  av  a  2  s. c om*/
    addContainerListeners(c, objs);
    AncestorListener ancestorListener = search(objs, AncestorListener.class);
    VetoableChangeListener vetoableChangeListener = search(objs, VetoableChangeListener.class);
    if (ancestorListener != null)
        c.addAncestorListener(ancestorListener);
    if (vetoableChangeListener != null)
        c.addVetoableChangeListener(vetoableChangeListener);
}

From source file:phex.gui.common.FileDialogHandler.java

/**
 * @param chooser/*  ww w  .ja va2s.c o  m*/
 */
private static void displayNotificationPopup(JComponent chooser, String title, String shortMessage) {
    final SlideInWindow window = new SlideInWindow(title, 0);
    window.setShortMessage(shortMessage, true);
    window.setHideBtnShown(false);
    window.initializeComponent();
    window.setSize(200, 150);
    chooser.addAncestorListener(new AncestorListener() {
        public void ancestorAdded(AncestorEvent event) {
            window.setVisible(true);
        }

        public void ancestorRemoved(AncestorEvent event) {
            window.setVisible(false);
        }

        public void ancestorMoved(AncestorEvent event) {
            Container ancestor = event.getAncestor();
            Point loc = ancestor.getLocationOnScreen();
            int xPos = loc.x + ancestor.getWidth() + 5;
            int yPos = loc.y + ancestor.getHeight() - window.getHeight();
            window.setLocation(xPos, yPos);
        }

    });
}