Example usage for javax.swing SwingUtilities getWindowAncestor

List of usage examples for javax.swing SwingUtilities getWindowAncestor

Introduction

In this page you can find the example usage for javax.swing SwingUtilities getWindowAncestor.

Prototype

public static Window getWindowAncestor(Component c) 

Source Link

Document

Returns the first Window ancestor of c, or null if c is not contained inside a Window.

Usage

From source file:Main.java

public static void main(String[] a) {
    JPanel panel = new JPanel();

    JDialog dialog = new JDialog((JFrame) SwingUtilities.getWindowAncestor(panel), true);
}

From source file:Main.java

public static void main(String[] args) {
    final JLabel label = new JLabel();
    int timerDelay = 1000;
    new Timer(timerDelay, new ActionListener() {
        int timeLeft = 5;

        @Override/* w  w  w . ja  va 2 s .  c  o m*/
        public void actionPerformed(ActionEvent e) {
            if (timeLeft > 0) {
                label.setText("Closing in " + timeLeft + " seconds");
                timeLeft--;
            } else {
                ((Timer) e.getSource()).stop();
                Window win = SwingUtilities.getWindowAncestor(label);
                win.setVisible(false);
            }
        }
    }) {
        {
            setInitialDelay(0);
        }
    }.start();

    JOptionPane.showMessageDialog(null, label);
}

From source file:Main.java

public static Component getRootWindow(Component c) {
    return SwingUtilities.getWindowAncestor(c);
}

From source file:Main.java

public static void setWindowOpacity(Component c, float opacity) {
    setWindowOpacity(SwingUtilities.getWindowAncestor(c), opacity);
}

From source file:Main.java

static Window getWindowAncestor(final Component component) {
    if (component == null) {
        return null;
    } else {//w w  w.  j a v  a  2s.c  o  m
        return SwingUtilities.getWindowAncestor(component);
    }
}

From source file:Main.java

static Window getComponentWindow(Component component) {
    if (component instanceof Window) {
        return (Window) component;
    }// www . j a  v a2 s  .  co m
    return SwingUtilities.getWindowAncestor(component);
}

From source file:Main.java

public static Window getWindowAncestorOrSelf(Component c) {
    if (c instanceof Window) {
        return (Window) c;
    }/*from   w w w  .j av a2  s.  c  o m*/
    if (c == null) {
        return null;
    }
    return SwingUtilities.getWindowAncestor(c);
}

From source file:Main.java

/**
 * Gets the layered pane of the window of the given component.
 * The window of the root pane should be a javax.swing.JFrame, javax.swing.JDialog
 * or javax.swing.JWindow. Otherwise the layered pane of the rootpane is returned.
 * /*from  w w  w. j av a2s  . c  o m*/
 * @param    rootPane      The root pane whose layered pane is retrieved.
 * @return                The layered pane.
 */
public static JLayeredPane getLayeredPane(JRootPane rootPane) {

    // Get the window of the component.
    Window window = SwingUtilities.getWindowAncestor(rootPane);
    if (window != null) {
        // Get the layered pane if we can find one.
        if (window instanceof JFrame)
            return ((JFrame) window).getLayeredPane();
        if (window instanceof JDialog)
            return ((JDialog) window).getLayeredPane();
        if (window instanceof JWindow)
            return ((JWindow) window).getLayeredPane();
    }

    // Get the layered pane of the root pane immediately.
    return rootPane.getLayeredPane();

}

From source file:Main.java

public static JDialog createJDialog(Component parentComponent, String title, boolean modal)
        throws HeadlessException {

    Window window = SwingUtilities.getWindowAncestor(parentComponent);
    if (window instanceof Frame) {
        return new JDialog((Frame) window, title, modal);
    } else {//  w  w w. j a v  a2 s  .c  om
        return new JDialog((Dialog) window, title, modal);
    }
}

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  .  ja  va2 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);
                }
            }
        });
    }
}