Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 BSI Business Systems Integration AG. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.awt.Dialog; import java.awt.Frame; import java.awt.KeyboardFocusManager; import java.awt.Window; import java.util.TreeMap; public class Main { public static Window getOwnerForChildWindow() { Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow(); if (w != null) { return w; } w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); if (w != null) { return w; } /* * Priority level1 * modal dialog: +200 * non-modal dialog: +100 * frame: +0 * * Priority level2 * no owned windows: +10 */ TreeMap<Integer, Window> prioMap = new TreeMap<Integer, Window>(); for (Window cand : Window.getWindows()) { if (cand == null) { continue; } if (!cand.isVisible()) { continue; } if (!cand.isShowing()) { continue; } int prio = 0; Window[] children = cand.getOwnedWindows(); if (children == null || children.length == 0) { prio += 10; } if (cand instanceof Dialog) { Dialog dlg = (Dialog) cand; if (dlg.isModal()) { prio += 200; } else { prio += 100; } prioMap.put(prio, cand); } else if (cand instanceof Frame) { if (!prioMap.containsKey(prio)) { prioMap.put(prio, cand); } } } if (prioMap.size() > 0) { return prioMap.get(prioMap.lastKey()); } //last line of defense if (prioMap.size() == 0) { for (Window cand : Window.getWindows()) { if (cand == null) { continue; } if (cand.isVisible()) { return cand; } } } return null; } }