/* * @(#)JDialog.java 1.80 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing; import java.awt.*; import java.awt.event.*; import java.beans.PropertyChangeListener; import java.util.Locale; import java.util.Vector; import java.io.Serializable; import javax.accessibility.*; import java.applet.Applet; /** * The main class for creating a dialog window. You can use this class * to create a custom dialog, or invoke the many class methods * in {@link JOptionPane} to create a variety of standard dialogs. * For information about creating dialogs, see * The Java Tutorial section * How * to Make Dialogs. * *

* * The JDialog component contains a JRootPane * as its only child. * The contentPane should be the parent of any children of the * JDialog. * As a conveniance add and its variants, remove and * setLayout have been overridden to forward to the * contentPane as necessary. This means you can write: *

 *       dialog.add(child);
 * 
* And the child will be added to the contentPane. * The contentPane is always non-null. * Attempting to set it to null generates an exception. * The default contentPane has a BorderLayout * manager set on it. * Refer to {@link javax.swing.RootPaneContainer} * for details on adding, removing and setting the LayoutManager * of a JDialog. *

* Please see the JRootPane documentation for a complete * description of the contentPane, glassPane, * and layeredPane components. *

* In a multi-screen environment, you can create a JDialog * on a different screen device than its owner. See {@link java.awt.Frame} for * more information. *

* Warning: * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeansTM * has been added to the java.beans package. * Please see {@link java.beans.XMLEncoder}. * * @see JOptionPane * @see JRootPane * @see javax.swing.RootPaneContainer * * @beaninfo * attribute: isContainer true * attribute: containerDelegate getContentPane * description: A toplevel window for creating dialog boxes. * * @version 1.80 12/19/03 * @author David Kloba * @author James Gosling * @author Scott Violet */ public class JDialog extends Dialog implements WindowConstants, Accessible, RootPaneContainer { /** * Key into the AppContext, used to check if should provide decorations * by default. */ private static final Object defaultLookAndFeelDecoratedKey = new StringBuffer("JDialog.defaultLookAndFeelDecorated"); private int defaultCloseOperation = HIDE_ON_CLOSE; /** * @see #getRootPane * @see #setRootPane */ protected JRootPane rootPane; /** * If true then calls to add and setLayout * will be forwarded to the contentPane. This is initially * false, but is set to true when the JDialog is constructed. * * @see #isRootPaneCheckingEnabled * @see #setRootPaneCheckingEnabled * @see javax.swing.RootPaneContainer */ protected boolean rootPaneCheckingEnabled = false; /** * Creates a non-modal dialog without a title and without a specified * Frame owner. A shared, hidden frame will be * set as the owner of the dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog() throws HeadlessException { this((Frame)null, false); } /** * Creates a non-modal dialog without a title with the * specified Frame as its owner. If owner * is null, a shared, hidden frame will be set as the * owner of the dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Frame from which the dialog is displayed * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Frame owner) throws HeadlessException { this(owner, false); } /** * Creates a modal or non-modal dialog without a title and * with the specified owner Frame. If owner * is null, a shared, hidden frame will be set as the * owner of the dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Frame from which the dialog is displayed * @param modal true for a modal dialog, false for one that allows * others windows to be active at the same time * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Frame owner, boolean modal) throws HeadlessException { this(owner, null, modal); } /** * Creates a non-modal dialog with the specified title and * with the specified owner frame. If owner * is null, a shared, hidden frame will be set as the * owner of the dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Frame from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Frame owner, String title) throws HeadlessException { this(owner, title, false); } /** * Creates a modal or non-modal dialog with the specified title * and the specified owner Frame. If owner * is null, a shared, hidden frame will be set as the * owner of this dialog. All constructors defer to this one. *

* NOTE: Any popup components (JComboBox, * JPopupMenu, JMenuBar) * created within a modal dialog will be forced to be lightweight. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Frame from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @param modal true for a modal dialog, false for one that allows * other windows to be active at the same time * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Frame owner, String title, boolean modal) throws HeadlessException { super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, title, modal); if (owner == null) { WindowListener ownerShutdownListener = (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener(); addWindowListener(ownerShutdownListener); } dialogInit(); } /** * Creates a modal or non-modal dialog with the specified title, * owner Frame, and GraphicsConfiguration. * *

* NOTE: Any popup components (JComboBox, * JPopupMenu, JMenuBar) * created within a modal dialog will be forced to be lightweight. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Frame from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @param modal true for a modal dialog, false for one that allows * other windows to be active at the same time * @param gc the GraphicsConfiguration * of the target screen device. If gc is * null, the same * GraphicsConfiguration as the owning Frame is used. * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale * @since 1.4 */ public JDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) { super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner, title, modal, gc); if (owner == null) { WindowListener ownerShutdownListener = (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener(); addWindowListener(ownerShutdownListener); } dialogInit(); } /** * Creates a non-modal dialog without a title with the * specified Dialog as its owner. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the non-null Dialog from which the dialog is displayed * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Dialog owner) throws HeadlessException { this(owner, false); } /** * Creates a modal or non-modal dialog without a title and * with the specified owner dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the non-null Dialog from which the dialog is displayed * @param modal true for a modal dialog, false for one that allows * other windows to be active at the same time * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Dialog owner, boolean modal) throws HeadlessException { this(owner, null, modal); } /** * Creates a non-modal dialog with the specified title and * with the specified owner dialog. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the non-null Dialog from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Dialog owner, String title) throws HeadlessException { this(owner, title, false); } /** * Creates a modal or non-modal dialog with the specified title * and the specified owner frame. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the non-null Dialog from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @param modal true for a modal dialog, false for one that allows * other windows to be active at the same time * @exception HeadlessException if GraphicsEnvironment.isHeadless() * returns true. * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale */ public JDialog(Dialog owner, String title, boolean modal) throws HeadlessException { super(owner, title, modal); dialogInit(); } /** * Creates a modal or non-modal dialog with the specified title, * owner Dialog, and GraphicsConfiguration. * *

* NOTE: Any popup components (JComboBox, * JPopupMenu, JMenuBar) * created within a modal dialog will be forced to be lightweight. *

* This constructor sets the component's locale property to the value * returned by JComponent.getDefaultLocale. * * @param owner the Dialog from which the dialog is displayed * @param title the String to display in the dialog's * title bar * @param modal true for a modal dialog, false for one that allows * other windows to be active at the same time * @param gc the GraphicsConfiguration * of the target screen device. If gc is * null, the same * GraphicsConfiguration as the owning Dialog is used. * @exception HeadlessException if GraphicsEnvironment.isHeadless() * @see java.awt.GraphicsEnvironment#isHeadless * @see JComponent#getDefaultLocale * returns true. * @since 1.4 */ public JDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) throws HeadlessException { super(owner, title, modal, gc); dialogInit(); } /** * Called by the constructors to init the JDialog properly. */ protected void dialogInit() { enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK); setLocale( JComponent.getDefaultLocale() ); setRootPane(createRootPane()); setRootPaneCheckingEnabled(true); if (JDialog.isDefaultLookAndFeelDecorated()) { boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations(); if (supportsWindowDecorations) { setUndecorated(true); getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); } } sun.awt.SunToolkit.checkAndSetPolicy(this, true); } /** * Called by the constructor methods to create the default * rootPane. */ protected JRootPane createRootPane() { JRootPane rp = new JRootPane(); // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there // is NO reason for the RootPane not to be opaque. For painting to // work the contentPane must be opaque, therefor the RootPane can // also be opaque. rp.setOpaque(true); return rp; } /** * Handles window events depending on the state of the * defaultCloseOperation property. * * @see #setDefaultCloseOperation */ protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { switch(defaultCloseOperation) { case HIDE_ON_CLOSE: setVisible(false); break; case DISPOSE_ON_CLOSE: setVisible(false); dispose(); break; case DO_NOTHING_ON_CLOSE: default: break; } } } /** * Sets the operation which will happen by default when * the user initiates a "close" on this dialog. * The possible choices are: *

*

* The value is set to HIDE_ON_CLOSE by default. *

* Note: When the last displayable window within the * Java virtual machine (VM) is disposed of, the VM may * terminate. See * AWT Threading Issues for more information. * @see #addWindowListener * @see #getDefaultCloseOperation * * @beaninfo * preferred: true * description: The dialog's default close operation. */ public void setDefaultCloseOperation(int operation) { this.defaultCloseOperation = operation; } /** * Returns the operation which occurs when the user * initiates a "close" on this dialog. * * @return an integer indicating the window-close operation * @see #setDefaultCloseOperation */ public int getDefaultCloseOperation() { return defaultCloseOperation; } /** * Calls paint(g). This method was overridden to * prevent an unnecessary call to clear the background. * * @param g the Graphics context in which to paint */ public void update(Graphics g) { paint(g); } /** * Sets the menubar for this dialog. * * @param menu the menubar being placed in the dialog * * @see #getJMenuBar * * @beaninfo * hidden: true * description: The menubar for accessing pulldown menus from this dialog. */ public void setJMenuBar(JMenuBar menu) { getRootPane().setMenuBar(menu); } /** * Returns the menubar set on this dialog. * * @see #setJMenuBar */ public JMenuBar getJMenuBar() { return getRootPane().getMenuBar(); } /** * Returns whether calls to add and * setLayout are forwarded to the contentPane. * * @return true if add and setLayout * are fowarded; false otherwise * * @see #addImpl * @see #setLayout * @see #setRootPaneCheckingEnabled * @see javax.swing.RootPaneContainer */ protected boolean isRootPaneCheckingEnabled() { return rootPaneCheckingEnabled; } /** * Sets whether calls to add and * setLayout are forwarded to the contentPane. * * @param enabled true if add and setLayout * are forwarded, false if they should operate directly on the * JDialog. * * @see #addImpl * @see #setLayout * @see #isRootPaneCheckingEnabled * @see javax.swing.RootPaneContainer * @beaninfo * hidden: true * description: Whether the add and setLayout methods are forwarded */ protected void setRootPaneCheckingEnabled(boolean enabled) { rootPaneCheckingEnabled = enabled; } /** * Adds the specified child Component. * This method is overridden to conditionally forwad calls to the * contentPane. * By default, children are added to the contentPane instead * of the frame, refer to {@link javax.swing.RootPaneContainer} for * details. * * @param comp the component to be enhanced * @param constraints the constraints to be respected * @param index the index * @exception IllegalArgumentException if index is invalid * @exception IllegalArgumentException if adding the container's parent * to itself * @exception IllegalArgumentException if adding a window to a container * * @see #setRootPaneCheckingEnabled * @see javax.swing.RootPaneContainer */ protected void addImpl(Component comp, Object constraints, int index) { if(isRootPaneCheckingEnabled()) { getContentPane().add(comp, constraints, index); } else { super.addImpl(comp, constraints, index); } } /** * Removes the specified component from the container. If * comp is not the rootPane, this will forward * the call to the contentPane. This will do nothing if * comp is not a child of the JDialog or * contentPane. * * @param comp the component to be removed * @throws NullPointerException if comp is null * @see #add * @see javax.swing.RootPaneContainer */ public void remove(Component comp) { if (comp == rootPane) { super.remove(comp); } else { getContentPane().remove(comp); } } /** * Sets the LayoutManager. * Overridden to conditionally forward the call to the * contentPane. * Refer to {@link javax.swing.RootPaneContainer} for * more information. * * @param manager the LayoutManager * @see #setRootPaneCheckingEnabled * @see javax.swing.RootPaneContainer */ public void setLayout(LayoutManager manager) { if(isRootPaneCheckingEnabled()) { getContentPane().setLayout(manager); } else { super.setLayout(manager); } } /** * Returns the rootPane object for this dialog. * * @see #setRootPane * @see RootPaneContainer#getRootPane */ public JRootPane getRootPane() { return rootPane; } /** * Sets the rootPane property. * This method is called by the constructor. * * @param root the rootPane object for this dialog * * @see #getRootPane * * @beaninfo * hidden: true * description: the RootPane object for this dialog. */ protected void setRootPane(JRootPane root) { if(rootPane != null) { remove(rootPane); } rootPane = root; if(rootPane != null) { boolean checkingEnabled = isRootPaneCheckingEnabled(); try { setRootPaneCheckingEnabled(false); add(rootPane, BorderLayout.CENTER); } finally { setRootPaneCheckingEnabled(checkingEnabled); } } } /** * Returns the contentPane object for this dialog. * * @return the contentPane property * * @see #setContentPane * @see RootPaneContainer#getContentPane */ public Container getContentPane() { return getRootPane().getContentPane(); } /** * Sets the contentPane property. * This method is called by the constructor. *

* Swing's painting architecture requires an opaque JComponent * in the containment hiearchy. This is typically provided by the * content pane. If you replace the content pane it is recommended you * replace it with an opaque JComponent. * @see JRootPane * * @param contentPane the contentPane object for this dialog * * @exception java.awt.IllegalComponentStateException (a runtime * exception) if the content pane parameter is null * @see #getContentPane * @see RootPaneContainer#setContentPane * * @beaninfo * hidden: true * description: The client area of the dialog where child * components are normally inserted. */ public void setContentPane(Container contentPane) { getRootPane().setContentPane(contentPane); } /** * Returns the layeredPane object for this dialog. * * @return the layeredPane property * * @see #setLayeredPane * @see RootPaneContainer#getLayeredPane */ public JLayeredPane getLayeredPane() { return getRootPane().getLayeredPane(); } /** * Sets the layeredPane property. * This method is called by the constructor. * * @param layeredPane the new layeredPane property * * @exception java.awt.IllegalComponentStateException (a runtime * exception) if the layered pane parameter is null * @see #getLayeredPane * @see RootPaneContainer#setLayeredPane * * @beaninfo * hidden: true * description: The pane which holds the various dialog layers. */ public void setLayeredPane(JLayeredPane layeredPane) { getRootPane().setLayeredPane(layeredPane); } /** * Returns the glassPane object for this dialog. * * @return the glassPane property * * @see #setGlassPane * @see RootPaneContainer#getGlassPane */ public Component getGlassPane() { return getRootPane().getGlassPane(); } /** * Sets the glassPane property. * This method is called by the constructor. * * @param glassPane the glassPane object for this dialog * @see #getGlassPane * @see RootPaneContainer#setGlassPane * * @beaninfo * hidden: true * description: A transparent pane used for menu rendering. */ public void setGlassPane(Component glassPane) { getRootPane().setGlassPane(glassPane); } /** * Provides a hint as to whether or not newly created JDialogs * should have their Window decorations (such as borders, widgets to * close the window, title...) provided by the current look * and feel. If defaultLookAndFeelDecorated is true, * the current LookAndFeel supports providing window * decorations, and the current window manager supports undecorated * windows, then newly created JDialogs will have their * Window decorations provided by the current LookAndFeel. * Otherwise, newly created JDialogs will have their * Window decorations provided by the current window manager. *

* You can get the same effect on a single JDialog by doing the following: *

     *    JDialog dialog = new JDialog();
     *    dialog.setUndecorated(true);
     *    dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
     * 
* * @param defaultLookAndFeelDecorated A hint as to whether or not current * look and feel should provide window decorations * @see javax.swing.LookAndFeel#getSupportsWindowDecorations * @since 1.4 */ public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) { if (defaultLookAndFeelDecorated) { SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE); } else { SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE); } } /** * Returns true if newly created JDialogs should have their * Window decorations provided by the current look and feel. This is only * a hint, as certain look and feels may not support this feature. * * @return true if look and feel should provide Window decorations. * @since 1.4 */ public static boolean isDefaultLookAndFeelDecorated() { Boolean defaultLookAndFeelDecorated = (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey); if (defaultLookAndFeelDecorated == null) { defaultLookAndFeelDecorated = Boolean.FALSE; } return defaultLookAndFeelDecorated.booleanValue(); } /** * Returns a string representation of this JDialog. * This method * is intended to be used only for debugging purposes, and the * content and format of the returned string may vary between * implementations. The returned string may be empty but may not * be null. * * @return a string representation of this JDialog. */ protected String paramString() { String defaultCloseOperationString; if (defaultCloseOperation == HIDE_ON_CLOSE) { defaultCloseOperationString = "HIDE_ON_CLOSE"; } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) { defaultCloseOperationString = "DISPOSE_ON_CLOSE"; } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) { defaultCloseOperationString = "DO_NOTHING_ON_CLOSE"; } else defaultCloseOperationString = ""; String rootPaneString = (rootPane != null ? rootPane.toString() : ""); String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? "true" : "false"); return super.paramString() + ",defaultCloseOperation=" + defaultCloseOperationString + ",rootPane=" + rootPaneString + ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString; } ///////////////// // Accessibility support //////////////// protected AccessibleContext accessibleContext = null; /** * Gets the AccessibleContext associated with this JDialog. * For JDialogs, the AccessibleContext takes the form of an * AccessibleJDialog. * A new AccessibleJDialog instance is created if necessary. * * @return an AccessibleJDialog that serves as the * AccessibleContext of this JDialog */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) { accessibleContext = new AccessibleJDialog(); } return accessibleContext; } /** * This class implements accessibility support for the * JDialog class. It provides an implementation of the * Java Accessibility API appropriate to dialog user-interface * elements. */ protected class AccessibleJDialog extends AccessibleAWTDialog { // AccessibleContext methods // /** * Get the accessible name of this object. * * @return the localized name of the object -- can be null if this * object does not have a name */ public String getAccessibleName() { if (accessibleName != null) { return accessibleName; } else { if (getTitle() == null) { return super.getAccessibleName(); } else { return getTitle(); } } } /** * Get the state of this object. * * @return an instance of AccessibleStateSet containing the current * state set of the object * @see AccessibleState */ public AccessibleStateSet getAccessibleStateSet() { AccessibleStateSet states = super.getAccessibleStateSet(); if (isResizable()) { states.add(AccessibleState.RESIZABLE); } if (getFocusOwner() != null) { states.add(AccessibleState.ACTIVE); } if (isModal()) { states.add(AccessibleState.MODAL); } return states; } } // inner class AccessibleJDialog }