Java Window getWindow(final Object source)

Here you can find the source of getWindow(final Object source)

Description

get Window

License

Apache License

Declaration

public static Window getWindow(final Object source) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.awt.Component;

import java.awt.MenuContainer;
import java.awt.MenuItem;
import java.awt.Window;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import javax.swing.SwingUtilities;

public class Main {
    public static Window getWindow(final Object source) {

        if (source instanceof JMenuItem) {
            final JMenuItem menuItem = (JMenuItem) source;
            MenuContainer menuContainer = menuItem.getParent();
            while (menuContainer != null && !(menuContainer instanceof JPopupMenu)) {
                if (menuContainer instanceof MenuItem) {
                    menuContainer = ((MenuItem) menuContainer).getParent();
                } else {
                    menuContainer = null;
                }/*from   www .ja  v  a2  s  .co m*/
            }
            if (menuContainer != null) {
                final JPopupMenu menu = (JPopupMenu) menuContainer;
                final Component invoker = menu.getInvoker();
                return SwingUtilities.getWindowAncestor(invoker);
            }

        } else if (source instanceof Component) {
            return SwingUtilities.getWindowAncestor((Component) source);
        }
        return null;
    }
}

Related

  1. dock(Window window, Window dockTo)
  2. enableCloseWindowWithEscape(final W window)
  3. fadeOut(final Window window, final boolean dispose)
  4. flashMessage(final Window parent, String string)
  5. getVisibleWindowByName(String name)
  6. getWindow(String title)
  7. getWindowNormalBounds(Window window)
  8. getWindowTitle(Window window)
  9. getWindowTitle(Window window)