Example usage for javax.swing.event AncestorEvent getAncestor

List of usage examples for javax.swing.event AncestorEvent getAncestor

Introduction

In this page you can find the example usage for javax.swing.event AncestorEvent getAncestor.

Prototype

public Container getAncestor() 

Source Link

Document

Returns the ancestor that the event actually occurred on.

Usage

From source file:Main.java

public static void main(String args[]) {
    final JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JButton b = new JButton("Hide for 5");
    ActionListener action = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            frame.setVisible(false);/*from   w  w  w  .  j a  v  a 2s. com*/
            TimerTask task = new TimerTask() {
                public void run() {
                    frame.setVisible(true);
                }
            };
            Timer timer = new Timer();
            timer.schedule(task, 5000);
        }
    };
    b.addActionListener(action);
    AncestorListener ancestor = new AncestorListener() {
        public void ancestorAdded(AncestorEvent e) {
            System.out.println("Added");
            dumpInfo(e);
        }

        public void ancestorMoved(AncestorEvent e) {
            System.out.println("Moved");
            dumpInfo(e);
        }

        public void ancestorRemoved(AncestorEvent e) {
            System.out.println("Removed");
            dumpInfo(e);
        }

        private void dumpInfo(AncestorEvent e) {
            System.out.println("\tAncestor: " + name(e.getAncestor()));
            System.out.println("\tAncestorParent: " + name(e.getAncestorParent()));
            System.out.println("\tComponent: " + name(e.getComponent()));
        }

        private String name(Container c) {
            return (c == null) ? null : c.getName();
        }
    };
    b.addAncestorListener(ancestor);
    contentPane.add(b, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

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

/**
 * @param chooser//from   w w  w  .j  a v  a2 s .  c om
 */
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);
        }

    });
}