Example usage for javax.swing SwingUtilities windowForComponent

List of usage examples for javax.swing SwingUtilities windowForComponent

Introduction

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

Prototype

public static Window windowForComponent(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[] args) {
    JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Click me to open dialog");
    button.addActionListener(e -> {/*from w w w  .ja v a 2  s. co m*/
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(new JLabel("A dialog"));
        dialog.pack();
        dialog.setVisible(true);
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static Dialog getDialog(Component c) {
    return (Dialog) SwingUtilities.windowForComponent(c);
}

From source file:Main.java

/**
 * Returns the frame that contains the given component.
 *
 * @param component component./*w w w .  j  a  va  2  s  . c om*/
 *
 * @return frame that contains the given component or <code>null</code> if there is
 *         no parent frame.
 */
public static Frame getOwnerFrame(Component component) {
    Window window = SwingUtilities.windowForComponent(component);
    Frame mother = null;

    if (window != null) {
        Window owner = window.getOwner();

        if ((owner != null) && owner instanceof Frame) {
            mother = (Frame) owner;
        }
    }

    return mother;
}

From source file:Main.java

public static void disposeParentWindow(JComponent component) {
    Window window = SwingUtilities.windowForComponent(component);
    if (window != null)
        window.dispose();//w w w . ja  v  a2  s . co  m
}

From source file:Main.java

private static JButton newButton(String label) {
    final JButton button = new JButton(label);
    button.addActionListener(e -> {//from   w w w.  ja  v  a2 s .  com
        Window parentWindow = SwingUtilities.windowForComponent(button);
        JDialog dialog = new JDialog(parentWindow);
        dialog.setLocationRelativeTo(button);
        dialog.setModal(true);
        dialog.add(newPane("Label"));
        dialog.pack();
        dialog.setVisible(true);
    });
    return button;
}

From source file:Main.java

public static Frame getFrame(Component c) {
    Frame frame;// w  ww . java 2s .  c o  m
    if (c instanceof Frame) {
        frame = (Frame) c;
    } else {
        frame = (Frame) SwingUtilities.windowForComponent(c);
    }
    return frame;
}

From source file:Main.java

/**
 * Gets the root pane of the given component.
 * //  w  ww  . j  a  v a  2s .  c  o m
 * @param    component      The component whose root pane is retrieved.
 * @return                The root pane of the component.
 */
public static JRootPane getRootPane(Component component) {

    if (component instanceof JRootPane) {
        return (JRootPane) component;
    }
    if (component.getParent() != null) {
        return getRootPane(component.getParent());
    }

    // Get the window of the component.
    Window window = SwingUtilities.windowForComponent(component);
    return getRootPane(window);

}

From source file:Main.java

public static Window getParentWindowForComponent(Component comp) {
    return SwingUtilities.windowForComponent(comp);
}

From source file:Main.java

public static int getMonitor(Component c) {
    Window w = (c instanceof Window) ? (Window) c : SwingUtilities.windowForComponent(c);
    GraphicsConfiguration gc = (w == null) ? null : w.getGraphicsConfiguration();
    GraphicsDevice gd = (gc == null) ? null : gc.getDevice();
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();

    for (int i = 0; i < gs.length; i++) {
        if (gs[i].equals(gd)) {
            return i;
        }/*from  w  w  w .j  av a 2  s.c  om*/
    }

    return -1;
}

From source file:GUIUtils.java

/**
 * Return the owning <CODE>Frame</CODE> for the passed component of <CODE>null</CODE>
 * if it doesn't have one.//from ww w  . ja v  a  2  s.  co m
 * 
 * @throws IllegalArgumentException
 *           If <TT>wind</TT> is <TT>null</TT>.
 */
public static Frame getOwningFrame(Component comp) {
    if (comp == null) {
        throw new IllegalArgumentException("null Component passed");
    }

    if (comp instanceof Frame) {
        return (Frame) comp;
    }
    return getOwningFrame(SwingUtilities.windowForComponent(comp));
}