下面列出了java.awt.Window#getOwner ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private JSONObject addWindowParents(JSONObject r, Window parent, JSONObject current) {
while (parent != null) {
if (!parent.getClass().getName().equals("javax.swing.SwingUtilities$SharedOwnerFrame")
&& !parent.getClass().getName().equals("javax.swing.Popup$HeavyWeightWindow") && parent.isVisible()) {
JSONObject pWindow = getContextJSONObject(parent);
if (r == null) {
r = pWindow;
}
if (current != null) {
current.put("container", pWindow);
}
current = pWindow;
}
parent = parent.getOwner();
}
return r;
}
/**
* Are we painting against a dark background? This checks the JVM version,
* the os, and whether the window's ultimate parent uses Apple's
* brush-metal-look.
*/
protected static boolean isDarkBackground(Window w) {
if (!isMac)
return false;
while (w != null) {
if (w instanceof RootPaneContainer) {
JRootPane rootPane = ((RootPaneContainer) w).getRootPane();
Object obj = rootPane
.getClientProperty("apple.awt.brushMetalLook");
if (obj == null)
obj = Boolean.FALSE;
if (obj.toString().equals("true")) {
return true;
}
}
w = w.getOwner();
}
return false;
}
/**
* Checks if the focus is still on this component or its child components.
*/
private boolean isFocusInside(Object newFocusedComp) {
if (newFocusedComp instanceof Popup) {
return true;
}
if (newFocusedComp instanceof Component && !SwingUtilities.isDescendingFrom((Component) newFocusedComp, this)) {
// Check if focus is on other window
if (containingWindow == null) {
return false;
}
Window focusedWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
// if focus is on other window in owner chain, return false
Window parent = containingWindow;
do {
if (parent == focusedWindow) {
return false;
} else {
parent = parent.getOwner();
}
} while (parent != null);
}
return true;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* @return Focused and showing Window or null.
*/
private Window findFocusedWindow() {
Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
while( null != w && !w.isShowing() ) {
w = w.getOwner();
}
return w;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Center on parent ( absolute true/false (exact center or 25% upper left) )
*
* @param child
* @param absolute
*/
public static void centerOnParent(final Window child, final boolean absolute) {
child.pack();
boolean useChildsOwner = child.getOwner() != null
? ((child.getOwner() instanceof JFrame) || (child.getOwner() instanceof JDialog))
: false;
final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final Dimension parentSize = useChildsOwner ? child.getOwner().getSize() : screenSize;
final Point parentLocationOnScreen =
useChildsOwner ? child.getOwner().getLocationOnScreen() : new Point(0, 0);
final Dimension childSize = child.getSize();
childSize.width = Math.min(childSize.width, screenSize.width);
childSize.height = Math.min(childSize.height, screenSize.height);
child.setSize(childSize);
int x;
int y;
if ((child.getOwner() != null) && child.getOwner().isShowing()) {
x = (parentSize.width - childSize.width) / 2;
y = (parentSize.height - childSize.height) / 2;
x += parentLocationOnScreen.x;
y += parentLocationOnScreen.y;
} else {
x = (screenSize.width - childSize.width) / 2;
y = (screenSize.height - childSize.height) / 2;
}
if (!absolute) {
x /= 2;
y /= 2;
}
child.setLocation(x, y);
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}
/**
* Return the owning Frame for a given Window. Used in setFSWindow below
* to set the properties of the owning Frame when a Window goes
* into fullscreen mode.
*/
private Frame getToplevelOwner(Window w) {
Window owner = w;
while (owner != null) {
owner = owner.getOwner();
if (owner instanceof Frame) {
return (Frame) owner;
}
}
// could get here if passed Window is an owner-less Dialog
return null;
}