super.done(); dialog.dispose(); JOptionPane.showMessageDialog(dialog.getOwner(), "Message sent", "Success", JOptionPane.INFORMATION_MESSAGE);
private Window getOwningFrame(Dialog dialog) { while(true) { Window w = ((Dialog)dialog).getOwner(); if(w == null) return w; if(!(w instanceof Dialog)) return w; dialog = (Dialog)w; } }
/** * Tells whether <code>parent</code> is really a parent of <code>child</code> * or not. * @param parent a window which may be an ancestor of <code>child</code> * @param child a window which may be child of <code>parent</code> * @return <code>true</code> if <code>parent</code> is an * ancestor of <code>child</code> */ private boolean isParent( Window parent, Window child ){ Window temp = child.getOwner(); while( temp != null ){ if( temp == parent ) return true; temp = temp.getOwner(); } return false; }
/** * Tells whether <code>parent</code> is really a parent of <code>child</code> * or not. * @param parent a window which may be an ancestor of <code>child</code> * @param child a window which may be child of <code>parent</code> * @return <code>true</code> if <code>parent</code> is an * ancestor of <code>child</code> */ private boolean isParent( Window parent, Window child ){ Window temp = child.getOwner(); while( temp != null ){ if( temp == parent ) return true; temp = temp.getOwner(); } return false; }
/** * "Injects" windows's content to parent window */ public static void dock(Window window) { dock(window, window.getOwner()); } }
/** * @see Window#setLocationRelativeTo(java.awt.Component) * @see Window#getOwner() */ @Override public void setLocation() { this.window.setLocationRelativeTo(this.window.getOwner()); }
/** * Returns the frame that contains the given component. * * @param component component. * * @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; }
private static Rectangle getUsableDeviceBounds(Window window) { Window owner = window.getOwner(); GraphicsConfiguration gc = null; if (owner == null) { gc = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice().getDefaultConfiguration(); } else { gc = owner.getGraphicsConfiguration(); } Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); Rectangle bounds = gc.getBounds(); bounds.x += insets.left; bounds.y += insets.top; bounds.width -= (insets.left + insets.right); bounds.height -= (insets.top + insets.bottom); return bounds; }
private static Frame getFrameWindow() { Window activeWindow = EditorUtilities.getActiveWindow(); if( activeWindow == null ) { return getActiveFrameFromApp(); } if( activeWindow instanceof Frame ) { return (Frame)activeWindow; } return (Frame)activeWindow.getOwner(); }
import java.lang.ref.WeakReference; import javax.swing.JDialog; import javax.swing.JFrame; public class Test { public static void main(String[] args) throws Throwable { WeakReference<JDialog> ref = test(); Runtime.getRuntime().gc(); System.out.println(ref.get()==null? "collected": "still flying around"); } public static WeakReference<JDialog> test() throws Throwable { JDialog d = new JDialog(new JFrame()); WeakReference<JDialog> ref = new WeakReference<JDialog>(d); d.setVisible(true); d.dispose(); d.getOwner().dispose(); return ref; } }
/** * Returns the component hierarchy. * * @param comp * Component. * @return Component hierarchy string. */ public static String getHierarchy(Component comp) { StringBuffer buffer = new StringBuffer(); getHierarchy(comp, buffer, 0); while (true) { if (comp instanceof Window) { Window w = (Window) comp; comp = w.getOwner(); if (comp != null) { buffer.append("Owner --->\n"); getHierarchy(comp, buffer, 0); } } else { break; } } return buffer.toString(); }
window.setLocationRelativeTo( window.getOwner() );
import java.awt.Window; import javax.swing.JFrame; import javax.swing.JDialog; public class WhyNoDispose extends JDialog { public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { try { WhyNoDispose frame = new WhyNoDispose("my title"); frame.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); JFrame win = (JFrame) frame.getOwner(); win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); win.dispose(); // System.exit(0); } catch (Exception e) { e.printStackTrace(); } } }); } public WhyNoDispose(String title) { super(new JFrame(title), ModalityType.APPLICATION_MODAL); pack(); } }
/** * Updates the application icon. */ private void updateAppIcon() { Window window = getWindow(); if (window == null) { this.appIcon = null; return; } this.appIcon = null; while (window != null && appIcon == null) { java.util.List<Image> icons = window.getIconImages(); if (icons.size() == 0) { window = window.getOwner(); } else { int prefSize = SubstanceSizeUtils.getTitlePaneIconSize(); this.appIcon = SubstanceCoreUtilities.getScaledIconImage(icons, prefSize, prefSize); } } }
public static DockingFrame create(Component c, String groupName) { RootWindow rootWin = RootWindow.getRootContainer(c); Component window = rootWin.getRootContainer(); if (window instanceof DockingFrame) { window = ((Window) window).getOwner(); } //Applets are actually contained in a frame if (window instanceof Applet) { window = SwingUtilities.windowForComponent(window); } if (window instanceof Frame) { return new DockingFrame((Frame) window, groupName); } if (window instanceof Dialog) { return new DockingFrame((Dialog) window, groupName); } return null; }
public static void center(final Window window) { if (window == null) { return; } final Window owner = window.getOwner(); if (owner == null || owner.getBounds().equals(new Rectangle(0, 0, 0, 0))) { centerToScreen(window); return; } final int x = owner.getX() + owner.getWidth() / 2 - window.getWidth() / 2; final int y = owner.getY() + owner.getHeight() / 2 - window.getHeight() / 2; window.setLocation(x < 0 ? 0 : x, y < 0 ? 0 : y); }