/** * Returns the ancestor {@link Window} of this {@code Panel} or * {@code null} if the component is not (yet) placed in a * {@code Window}. */ public @Nullable Window getAncestorWindow() { return getAncestorWindow(this); }
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
final Window window = evt.getSource().getAncestorWindow(); window.addWindowFocusListener(new WindowFocusListener() { @Override
@Override public void hierarchyChanged(final HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != HierarchyEvent.SHOWING_CHANGED) return; final Window window = getAncestorWindow(); assert null != window : "A showing panel must have a containing window!"; final boolean windowShown = window.isShowing(); if (windowShown != isShowing()) return; processPanelEvent(new PanelEvent(EnhancedPanel.this, windowShown ? PanelEvent.ANCESTOR_WINDOW_SHOWN : PanelEvent.ANCESTOR_WINDOW_HIDDEN)); } } // class EnhancedPanelHierarchyListener
@Test public void testGetAncestorWindow() throws InterruptedException { assertNull(instance.getAncestorWindow()); JFrameOperator frame = showFrameWith(instance); Window window1 = instance.getAncestorWindow(); assertSame(frame.getSource(), window1); frame.dispose(); frame = showFrameWith(instance); // change enclosing frame Window window2 = instance.getAncestorWindow(); assertSame(frame.getSource(), window2); frame.dispose(); assertNotSame(window1, window2); }