KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > JDialog


1 /*
2  * @(#)JDialog.java 1.80 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.beans.PropertyChangeListener JavaDoc;
12 import java.util.Locale JavaDoc;
13 import java.util.Vector JavaDoc;
14 import java.io.Serializable JavaDoc;
15 import javax.accessibility.*;
16 import java.applet.Applet JavaDoc;
17
18 /**
19  * The main class for creating a dialog window. You can use this class
20  * to create a custom dialog, or invoke the many class methods
21  * in {@link JOptionPane} to create a variety of standard dialogs.
22  * For information about creating dialogs, see
23  * <em>The Java Tutorial</em> section
24  * <a
25  href="http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html">How
26  * to Make Dialogs</a>.
27  *
28  * <p>
29  *
30  * The <code>JDialog</code> component contains a <code>JRootPane</code>
31  * as its only child.
32  * The <code>contentPane</code> should be the parent of any children of the
33  * <code>JDialog</code>.
34  * As a conveniance <code>add</code> and its variants, <code>remove</code> and
35  * <code>setLayout</code> have been overridden to forward to the
36  * <code>contentPane</code> as necessary. This means you can write:
37  * <pre>
38  * dialog.add(child);
39  * </pre>
40  * And the child will be added to the contentPane.
41  * The <code>contentPane</code> is always non-<code>null</code>.
42  * Attempting to set it to <code>null</code> generates an exception.
43  * The default <code>contentPane</code> has a <code>BorderLayout</code>
44  * manager set on it.
45  * Refer to {@link javax.swing.RootPaneContainer}
46  * for details on adding, removing and setting the <code>LayoutManager</code>
47  * of a <code>JDialog</code>.
48  * <p>
49  * Please see the <code>JRootPane</code> documentation for a complete
50  * description of the <code>contentPane</code>, <code>glassPane</code>,
51  * and <code>layeredPane</code> components.
52  * <p>
53  * In a multi-screen environment, you can create a <code>JDialog</code>
54  * on a different screen device than its owner. See {@link java.awt.Frame} for
55  * more information.
56  * <p>
57  * <strong>Warning:</strong>
58  * Serialized objects of this class will not be compatible with
59  * future Swing releases. The current serialization support is
60  * appropriate for short term storage or RMI between applications running
61  * the same version of Swing. As of 1.4, support for long term storage
62  * of all JavaBeans<sup><font size="-2">TM</font></sup>
63  * has been added to the <code>java.beans</code> package.
64  * Please see {@link java.beans.XMLEncoder}.
65  *
66  * @see JOptionPane
67  * @see JRootPane
68  * @see javax.swing.RootPaneContainer
69  *
70  * @beaninfo
71  * attribute: isContainer true
72  * attribute: containerDelegate getContentPane
73  * description: A toplevel window for creating dialog boxes.
74  *
75  * @version 1.80 12/19/03
76  * @author David Kloba
77  * @author James Gosling
78  * @author Scott Violet
79  */

80 public class JDialog extends Dialog implements WindowConstants JavaDoc, Accessible, RootPaneContainer JavaDoc
81 {
82     /**
83      * Key into the AppContext, used to check if should provide decorations
84      * by default.
85      */

86     private static final Object JavaDoc defaultLookAndFeelDecoratedKey =
87             new StringBuffer JavaDoc("JDialog.defaultLookAndFeelDecorated");
88
89     private int defaultCloseOperation = HIDE_ON_CLOSE;
90     
91     /**
92      * @see #getRootPane
93      * @see #setRootPane
94      */

95     protected JRootPane JavaDoc rootPane;
96
97     /**
98      * If true then calls to <code>add</code> and <code>setLayout</code>
99      * will be forwarded to the <code>contentPane</code>. This is initially
100      * false, but is set to true when the <code>JDialog</code> is constructed.
101      *
102      * @see #isRootPaneCheckingEnabled
103      * @see #setRootPaneCheckingEnabled
104      * @see javax.swing.RootPaneContainer
105      */

106     protected boolean rootPaneCheckingEnabled = false;
107
108
109     /**
110      * Creates a non-modal dialog without a title and without a specified
111      * <code>Frame</code> owner. A shared, hidden frame will be
112      * set as the owner of the dialog.
113      * <p>
114      * This constructor sets the component's locale property to the value
115      * returned by <code>JComponent.getDefaultLocale</code>.
116      *
117      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
118      * returns true.
119      * @see java.awt.GraphicsEnvironment#isHeadless
120      * @see JComponent#getDefaultLocale
121      */

122     public JDialog() throws HeadlessException {
123         this((Frame)null, false);
124     }
125
126     /**
127      * Creates a non-modal dialog without a title with the
128      * specified <code>Frame</code> as its owner. If <code>owner</code>
129      * is <code>null</code>, a shared, hidden frame will be set as the
130      * owner of the dialog.
131      * <p>
132      * This constructor sets the component's locale property to the value
133      * returned by <code>JComponent.getDefaultLocale</code>.
134      *
135      * @param owner the <code>Frame</code> from which the dialog is displayed
136      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
137      * returns true.
138      * @see java.awt.GraphicsEnvironment#isHeadless
139      * @see JComponent#getDefaultLocale
140      */

141     public JDialog(Frame owner) throws HeadlessException {
142         this(owner, false);
143     }
144
145     /**
146      * Creates a modal or non-modal dialog without a title and
147      * with the specified owner <code>Frame</code>. If <code>owner</code>
148      * is <code>null</code>, a shared, hidden frame will be set as the
149      * owner of the dialog.
150      * <p>
151      * This constructor sets the component's locale property to the value
152      * returned by <code>JComponent.getDefaultLocale</code>.
153      *
154      * @param owner the <code>Frame</code> from which the dialog is displayed
155      * @param modal true for a modal dialog, false for one that allows
156      * others windows to be active at the same time
157      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
158      * returns true.
159      * @see java.awt.GraphicsEnvironment#isHeadless
160      * @see JComponent#getDefaultLocale
161      */

162     public JDialog(Frame owner, boolean modal) throws HeadlessException {
163         this(owner, null, modal);
164     }
165
166     /**
167      * Creates a non-modal dialog with the specified title and
168      * with the specified owner frame. If <code>owner</code>
169      * is <code>null</code>, a shared, hidden frame will be set as the
170      * owner of the dialog.
171      * <p>
172      * This constructor sets the component's locale property to the value
173      * returned by <code>JComponent.getDefaultLocale</code>.
174      *
175      * @param owner the <code>Frame</code> from which the dialog is displayed
176      * @param title the <code>String</code> to display in the dialog's
177      * title bar
178      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
179      * returns true.
180      * @see java.awt.GraphicsEnvironment#isHeadless
181      * @see JComponent#getDefaultLocale
182      */

183     public JDialog(Frame owner, String JavaDoc title) throws HeadlessException {
184         this(owner, title, false);
185     }
186
187     /**
188      * Creates a modal or non-modal dialog with the specified title
189      * and the specified owner <code>Frame</code>. If <code>owner</code>
190      * is <code>null</code>, a shared, hidden frame will be set as the
191      * owner of this dialog. All constructors defer to this one.
192      * <p>
193      * NOTE: Any popup components (<code>JComboBox</code>,
194      * <code>JPopupMenu</code>, <code>JMenuBar</code>)
195      * created within a modal dialog will be forced to be lightweight.
196      * <p>
197      * This constructor sets the component's locale property to the value
198      * returned by <code>JComponent.getDefaultLocale</code>.
199      *
200      * @param owner the <code>Frame</code> from which the dialog is displayed
201      * @param title the <code>String</code> to display in the dialog's
202      * title bar
203      * @param modal true for a modal dialog, false for one that allows
204      * other windows to be active at the same time
205      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
206      * returns true.
207      * @see java.awt.GraphicsEnvironment#isHeadless
208      * @see JComponent#getDefaultLocale
209      */

210     public JDialog(Frame owner, String JavaDoc title, boolean modal)
211         throws HeadlessException {
212         super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
213               title, modal);
214     if (owner == null) {
215         WindowListener ownerShutdownListener =
216         (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener();
217         addWindowListener(ownerShutdownListener);
218     }
219         dialogInit();
220     }
221
222     /**
223      * Creates a modal or non-modal dialog with the specified title,
224      * owner <code>Frame</code>, and <code>GraphicsConfiguration</code>.
225      *
226      * <p>
227      * NOTE: Any popup components (<code>JComboBox</code>,
228      * <code>JPopupMenu</code>, <code>JMenuBar</code>)
229      * created within a modal dialog will be forced to be lightweight.
230      * <p>
231      * This constructor sets the component's locale property to the value
232      * returned by <code>JComponent.getDefaultLocale</code>.
233      *
234      * @param owner the <code>Frame</code> from which the dialog is displayed
235      * @param title the <code>String</code> to display in the dialog's
236      * title bar
237      * @param modal true for a modal dialog, false for one that allows
238      * other windows to be active at the same time
239      * @param gc the <code>GraphicsConfiguration</code>
240      * of the target screen device. If <code>gc</code> is
241      * <code>null</code>, the same
242      * <code>GraphicsConfiguration</code> as the owning Frame is used.
243      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
244      * returns true.
245      * @see java.awt.GraphicsEnvironment#isHeadless
246      * @see JComponent#getDefaultLocale
247      * @since 1.4
248      */

249     public JDialog(Frame owner, String JavaDoc title, boolean modal,
250                    GraphicsConfiguration gc) {
251         super(owner == null? SwingUtilities.getSharedOwnerFrame() : owner,
252               title, modal, gc);
253     if (owner == null) {
254         WindowListener ownerShutdownListener =
255         (WindowListener)SwingUtilities.getSharedOwnerFrameShutdownListener();
256         addWindowListener(ownerShutdownListener);
257     }
258         dialogInit();
259     }
260
261     /**
262      * Creates a non-modal dialog without a title with the
263      * specified <code>Dialog</code> as its owner.
264      * <p>
265      * This constructor sets the component's locale property to the value
266      * returned by <code>JComponent.getDefaultLocale</code>.
267      *
268      * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
269      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
270      * returns true.
271      * @see java.awt.GraphicsEnvironment#isHeadless
272      * @see JComponent#getDefaultLocale
273      */

274     public JDialog(Dialog owner) throws HeadlessException {
275         this(owner, false);
276     }
277
278     /**
279      * Creates a modal or non-modal dialog without a title and
280      * with the specified owner dialog.
281      * <p>
282      * This constructor sets the component's locale property to the value
283      * returned by <code>JComponent.getDefaultLocale</code>.
284      *
285      * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
286      * @param modal true for a modal dialog, false for one that allows
287      * other windows to be active at the same time
288      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
289      * returns true.
290      * @see java.awt.GraphicsEnvironment#isHeadless
291      * @see JComponent#getDefaultLocale
292      */

293     public JDialog(Dialog owner, boolean modal) throws HeadlessException {
294         this(owner, null, modal);
295     }
296
297     /**
298      * Creates a non-modal dialog with the specified title and
299      * with the specified owner dialog.
300      * <p>
301      * This constructor sets the component's locale property to the value
302      * returned by <code>JComponent.getDefaultLocale</code>.
303      *
304      * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
305      * @param title the <code>String</code> to display in the dialog's
306      * title bar
307      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
308      * returns true.
309      * @see java.awt.GraphicsEnvironment#isHeadless
310      * @see JComponent#getDefaultLocale
311      */

312     public JDialog(Dialog owner, String JavaDoc title) throws HeadlessException {
313         this(owner, title, false);
314     }
315
316     /**
317      * Creates a modal or non-modal dialog with the specified title
318      * and the specified owner frame.
319      * <p>
320      * This constructor sets the component's locale property to the value
321      * returned by <code>JComponent.getDefaultLocale</code>.
322      *
323      * @param owner the non-null <code>Dialog</code> from which the dialog is displayed
324      * @param title the <code>String</code> to display in the dialog's
325      * title bar
326      * @param modal true for a modal dialog, false for one that allows
327      * other windows to be active at the same time
328      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
329      * returns true.
330      * @see java.awt.GraphicsEnvironment#isHeadless
331      * @see JComponent#getDefaultLocale
332      */

333     public JDialog(Dialog owner, String JavaDoc title, boolean modal)
334         throws HeadlessException {
335         super(owner, title, modal);
336         dialogInit();
337     }
338
339     /**
340      * Creates a modal or non-modal dialog with the specified title,
341      * owner <code>Dialog</code>, and <code>GraphicsConfiguration</code>.
342      *
343      * <p>
344      * NOTE: Any popup components (<code>JComboBox</code>,
345      * <code>JPopupMenu</code>, <code>JMenuBar</code>)
346      * created within a modal dialog will be forced to be lightweight.
347      * <p>
348      * This constructor sets the component's locale property to the value
349      * returned by <code>JComponent.getDefaultLocale</code>.
350      *
351      * @param owner the <code>Dialog</code> from which the dialog is displayed
352      * @param title the <code>String</code> to display in the dialog's
353      * title bar
354      * @param modal true for a modal dialog, false for one that allows
355      * other windows to be active at the same time
356      * @param gc the <code>GraphicsConfiguration</code>
357      * of the target screen device. If <code>gc</code> is
358      * <code>null</code>, the same
359      * <code>GraphicsConfiguration</code> as the owning Dialog is used.
360      * @exception HeadlessException if GraphicsEnvironment.isHeadless()
361      * @see java.awt.GraphicsEnvironment#isHeadless
362      * @see JComponent#getDefaultLocale
363      * returns true.
364      * @since 1.4
365      */

366     public JDialog(Dialog owner, String JavaDoc title, boolean modal,
367                    GraphicsConfiguration gc) throws HeadlessException {
368
369         super(owner, title, modal, gc);
370         dialogInit();
371     }
372
373     /**
374      * Called by the constructors to init the <code>JDialog</code> properly.
375      */

376     protected void dialogInit() {
377         enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
378         setLocale( JComponent.getDefaultLocale() );
379         setRootPane(createRootPane());
380         setRootPaneCheckingEnabled(true);
381         if (JDialog.isDefaultLookAndFeelDecorated()) {
382             boolean supportsWindowDecorations =
383             UIManager.getLookAndFeel().getSupportsWindowDecorations();
384             if (supportsWindowDecorations) {
385                 setUndecorated(true);
386                 getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
387             }
388         }
389         sun.awt.SunToolkit.checkAndSetPolicy(this, true);
390     }
391
392     /**
393      * Called by the constructor methods to create the default
394      * <code>rootPane</code>.
395      */

396     protected JRootPane JavaDoc createRootPane() {
397         JRootPane JavaDoc rp = new JRootPane JavaDoc();
398         // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
399
// is NO reason for the RootPane not to be opaque. For painting to
400
// work the contentPane must be opaque, therefor the RootPane can
401
// also be opaque.
402
rp.setOpaque(true);
403         return rp;
404     }
405
406     /**
407      * Handles window events depending on the state of the
408      * <code>defaultCloseOperation</code> property.
409      *
410      * @see #setDefaultCloseOperation
411      */

412     protected void processWindowEvent(WindowEvent e) {
413         super.processWindowEvent(e);
414
415         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
416             switch(defaultCloseOperation) {
417               case HIDE_ON_CLOSE:
418                  setVisible(false);
419                  break;
420               case DISPOSE_ON_CLOSE:
421                  setVisible(false);
422                  dispose();
423                  break;
424               case DO_NOTHING_ON_CLOSE:
425                  default:
426                  break;
427             }
428         }
429     }
430  
431
432     /**
433      * Sets the operation which will happen by default when
434      * the user initiates a "close" on this dialog.
435      * The possible choices are:
436      * <ul>
437      * <li><code>DO_NOTHING_ON_CLOSE</code> - do not do anything - require the
438      * program to handle the operation in the <code>windowClosing</code>
439      * method of a registered <code>WindowListener</code> object.
440      *
441      * <li><code>HIDE_ON_CLOSE</code> - automatically hide the dialog after
442      * invoking any registered <code>WindowListener</code> objects
443      *
444      * <li><code>DISPOSE_ON_CLOSE</code> - automatically hide and dispose the
445      * dialog after invoking any registered <code>WindowListener</code> objects
446      * </ul>
447      * <p>
448      * The value is set to <code>HIDE_ON_CLOSE</code> by default.
449      * <p>
450      * <b>Note</b>: When the last displayable window within the
451      * Java virtual machine (VM) is disposed of, the VM may
452      * terminate. See <a HREF="../../java/awt/doc-files/AWTThreadIssues.html">
453      * AWT Threading Issues</a> for more information.
454      * @see #addWindowListener
455      * @see #getDefaultCloseOperation
456      *
457      * @beaninfo
458      * preferred: true
459      * description: The dialog's default close operation.
460      */

461     public void setDefaultCloseOperation(int operation) {
462         this.defaultCloseOperation = operation;
463     }
464
465    /**
466     * Returns the operation which occurs when the user
467     * initiates a "close" on this dialog.
468     *
469     * @return an integer indicating the window-close operation
470     * @see #setDefaultCloseOperation
471     */

472     public int getDefaultCloseOperation() {
473         return defaultCloseOperation;
474     }
475
476
477     /**
478      * Calls <code>paint(g)</code>. This method was overridden to
479      * prevent an unnecessary call to clear the background.
480      *
481      * @param g the <code>Graphics</code> context in which to paint
482      */

483     public void update(Graphics g) {
484         paint(g);
485     }
486
487    /**
488     * Sets the menubar for this dialog.
489     *
490     * @param menu the menubar being placed in the dialog
491     *
492     * @see #getJMenuBar
493     *
494     * @beaninfo
495     * hidden: true
496     * description: The menubar for accessing pulldown menus from this dialog.
497     */

498     public void setJMenuBar(JMenuBar JavaDoc menu) {
499         getRootPane().setMenuBar(menu);
500     }
501
502    /**
503     * Returns the menubar set on this dialog.
504     *
505     * @see #setJMenuBar
506     */

507     public JMenuBar JavaDoc getJMenuBar() {
508         return getRootPane().getMenuBar();
509     }
510
511
512     /**
513      * Returns whether calls to <code>add</code> and
514      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
515      *
516      * @return true if <code>add</code> and <code>setLayout</code>
517      * are fowarded; false otherwise
518      *
519      * @see #addImpl
520      * @see #setLayout
521      * @see #setRootPaneCheckingEnabled
522      * @see javax.swing.RootPaneContainer
523      */

524     protected boolean isRootPaneCheckingEnabled() {
525         return rootPaneCheckingEnabled;
526     }
527
528
529     /**
530      * Sets whether calls to <code>add</code> and
531      * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
532      *
533      * @param enabled true if <code>add</code> and <code>setLayout</code>
534      * are forwarded, false if they should operate directly on the
535      * <code>JDialog</code>.
536      *
537      * @see #addImpl
538      * @see #setLayout
539      * @see #isRootPaneCheckingEnabled
540      * @see javax.swing.RootPaneContainer
541      * @beaninfo
542      * hidden: true
543      * description: Whether the add and setLayout methods are forwarded
544      */

545     protected void setRootPaneCheckingEnabled(boolean enabled) {
546         rootPaneCheckingEnabled = enabled;
547     }
548
549     /**
550      * Adds the specified child <code>Component</code>.
551      * This method is overridden to conditionally forwad calls to the
552      * <code>contentPane</code>.
553      * By default, children are added to the <code>contentPane</code> instead
554      * of the frame, refer to {@link javax.swing.RootPaneContainer} for
555      * details.
556      *
557      * @param comp the component to be enhanced
558      * @param constraints the constraints to be respected
559      * @param index the index
560      * @exception IllegalArgumentException if <code>index</code> is invalid
561      * @exception IllegalArgumentException if adding the container's parent
562      * to itself
563      * @exception IllegalArgumentException if adding a window to a container
564      *
565      * @see #setRootPaneCheckingEnabled
566      * @see javax.swing.RootPaneContainer
567      */

568     protected void addImpl(Component comp, Object JavaDoc constraints, int index)
569     {
570         if(isRootPaneCheckingEnabled()) {
571             getContentPane().add(comp, constraints, index);
572         }
573         else {
574             super.addImpl(comp, constraints, index);
575         }
576     }
577
578     /**
579      * Removes the specified component from the container. If
580      * <code>comp</code> is not the <code>rootPane</code>, this will forward
581      * the call to the <code>contentPane</code>. This will do nothing if
582      * <code>comp</code> is not a child of the <code>JDialog</code> or
583      * <code>contentPane</code>.
584      *
585      * @param comp the component to be removed
586      * @throws NullPointerException if <code>comp</code> is null
587      * @see #add
588      * @see javax.swing.RootPaneContainer
589      */

590     public void remove(Component comp) {
591     if (comp == rootPane) {
592         super.remove(comp);
593     } else {
594         getContentPane().remove(comp);
595     }
596     }
597
598
599     /**
600      * Sets the <code>LayoutManager</code>.
601      * Overridden to conditionally forward the call to the
602      * <code>contentPane</code>.
603      * Refer to {@link javax.swing.RootPaneContainer} for
604      * more information.
605      *
606      * @param manager the <code>LayoutManager</code>
607      * @see #setRootPaneCheckingEnabled
608      * @see javax.swing.RootPaneContainer
609      */

610     public void setLayout(LayoutManager manager) {
611         if(isRootPaneCheckingEnabled()) {
612             getContentPane().setLayout(manager);
613         }
614         else {
615             super.setLayout(manager);
616         }
617     }
618
619
620     /**
621      * Returns the <code>rootPane</code> object for this dialog.
622      *
623      * @see #setRootPane
624      * @see RootPaneContainer#getRootPane
625      */

626     public JRootPane JavaDoc getRootPane() {
627         return rootPane;
628     }
629
630
631     /**
632      * Sets the <code>rootPane</code> property.
633      * This method is called by the constructor.
634      *
635      * @param root the <code>rootPane</code> object for this dialog
636      *
637      * @see #getRootPane
638      *
639      * @beaninfo
640      * hidden: true
641      * description: the RootPane object for this dialog.
642      */

643     protected void setRootPane(JRootPane JavaDoc root) {
644         if(rootPane != null) {
645             remove(rootPane);
646         }
647         rootPane = root;
648         if(rootPane != null) {
649             boolean checkingEnabled = isRootPaneCheckingEnabled();
650             try {
651                 setRootPaneCheckingEnabled(false);
652                 add(rootPane, BorderLayout.CENTER);
653             }
654             finally {
655                 setRootPaneCheckingEnabled(checkingEnabled);
656             }
657         }
658     }
659
660
661     /**
662      * Returns the <code>contentPane</code> object for this dialog.
663      *
664      * @return the <code>contentPane</code> property
665      *
666      * @see #setContentPane
667      * @see RootPaneContainer#getContentPane
668      */

669     public Container getContentPane() {
670         return getRootPane().getContentPane();
671     }
672
673
674    /**
675      * Sets the <code>contentPane</code> property.
676      * This method is called by the constructor.
677      * <p>
678      * Swing's painting architecture requires an opaque <code>JComponent</code>
679      * in the containment hiearchy. This is typically provided by the
680      * content pane. If you replace the content pane it is recommended you
681      * replace it with an opaque <code>JComponent</code>.
682      * @see JRootPane
683      *
684      * @param contentPane the <code>contentPane</code> object for this dialog
685      *
686      * @exception java.awt.IllegalComponentStateException (a runtime
687      * exception) if the content pane parameter is <code>null</code>
688      * @see #getContentPane
689      * @see RootPaneContainer#setContentPane
690      *
691      * @beaninfo
692      * hidden: true
693      * description: The client area of the dialog where child
694      * components are normally inserted.
695      */

696     public void setContentPane(Container contentPane) {
697         getRootPane().setContentPane(contentPane);
698     }
699
700     /**
701      * Returns the <code>layeredPane</code> object for this dialog.
702      *
703      * @return the <code>layeredPane</code> property
704      *
705      * @see #setLayeredPane
706      * @see RootPaneContainer#getLayeredPane
707      */

708     public JLayeredPane JavaDoc getLayeredPane() {
709         return getRootPane().getLayeredPane();
710     }
711
712     /**
713      * Sets the <code>layeredPane</code> property.
714      * This method is called by the constructor.
715      *
716      * @param layeredPane the new <code>layeredPane</code> property
717      *
718      * @exception java.awt.IllegalComponentStateException (a runtime
719      * exception) if the layered pane parameter is null
720      * @see #getLayeredPane
721      * @see RootPaneContainer#setLayeredPane
722      *
723      * @beaninfo
724      * hidden: true
725      * description: The pane which holds the various dialog layers.
726      */

727     public void setLayeredPane(JLayeredPane JavaDoc layeredPane) {
728         getRootPane().setLayeredPane(layeredPane);
729     }
730
731     /**
732      * Returns the <code>glassPane</code> object for this dialog.
733      *
734      * @return the <code>glassPane</code> property
735      *
736      * @see #setGlassPane
737      * @see RootPaneContainer#getGlassPane
738      */

739     public Component getGlassPane() {
740         return getRootPane().getGlassPane();
741     }
742
743     /**
744      * Sets the <code>glassPane</code> property.
745      * This method is called by the constructor.
746      *
747      * @param glassPane the <code>glassPane</code> object for this dialog
748      * @see #getGlassPane
749      * @see RootPaneContainer#setGlassPane
750      *
751      * @beaninfo
752      * hidden: true
753      * description: A transparent pane used for menu rendering.
754      */

755     public void setGlassPane(Component glassPane) {
756         getRootPane().setGlassPane(glassPane);
757     }
758
759     /**
760      * Provides a hint as to whether or not newly created <code>JDialog</code>s
761      * should have their Window decorations (such as borders, widgets to
762      * close the window, title...) provided by the current look
763      * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
764      * the current <code>LookAndFeel</code> supports providing window
765      * decorations, and the current window manager supports undecorated
766      * windows, then newly created <code>JDialog</code>s will have their
767      * Window decorations provided by the current <code>LookAndFeel</code>.
768      * Otherwise, newly created <code>JDialog</code>s will have their
769      * Window decorations provided by the current window manager.
770      * <p>
771      * You can get the same effect on a single JDialog by doing the following:
772      * <pre>
773      * JDialog dialog = new JDialog();
774      * dialog.setUndecorated(true);
775      * dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
776      * </pre>
777      *
778      * @param defaultLookAndFeelDecorated A hint as to whether or not current
779      * look and feel should provide window decorations
780      * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
781      * @since 1.4
782      */

783     public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
784         if (defaultLookAndFeelDecorated) {
785             SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
786         } else {
787             SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
788         }
789     }
790
791     /**
792      * Returns true if newly created <code>JDialog</code>s should have their
793      * Window decorations provided by the current look and feel. This is only
794      * a hint, as certain look and feels may not support this feature.
795      *
796      * @return true if look and feel should provide Window decorations.
797      * @since 1.4
798      */

799     public static boolean isDefaultLookAndFeelDecorated() {
800         Boolean JavaDoc defaultLookAndFeelDecorated =
801             (Boolean JavaDoc) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
802         if (defaultLookAndFeelDecorated == null) {
803             defaultLookAndFeelDecorated = Boolean.FALSE;
804         }
805         return defaultLookAndFeelDecorated.booleanValue();
806     }
807
808     /**
809      * Returns a string representation of this <code>JDialog</code>.
810      * This method
811      * is intended to be used only for debugging purposes, and the
812      * content and format of the returned string may vary between
813      * implementations. The returned string may be empty but may not
814      * be <code>null</code>.
815      *
816      * @return a string representation of this <code>JDialog</code>.
817      */

818     protected String JavaDoc paramString() {
819         String JavaDoc defaultCloseOperationString;
820         if (defaultCloseOperation == HIDE_ON_CLOSE) {
821             defaultCloseOperationString = "HIDE_ON_CLOSE";
822         } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
823             defaultCloseOperationString = "DISPOSE_ON_CLOSE";
824         } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
825             defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
826         } else defaultCloseOperationString = "";
827     String JavaDoc rootPaneString = (rootPane != null ?
828                  rootPane.toString() : "");
829     String JavaDoc rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
830                         "true" : "false");
831
832     return super.paramString() +
833     ",defaultCloseOperation=" + defaultCloseOperationString +
834     ",rootPane=" + rootPaneString +
835     ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
836     }
837
838
839 /////////////////
840
// Accessibility support
841
////////////////
842

843     protected AccessibleContext accessibleContext = null;
844
845     /**
846      * Gets the AccessibleContext associated with this JDialog.
847      * For JDialogs, the AccessibleContext takes the form of an
848      * AccessibleJDialog.
849      * A new AccessibleJDialog instance is created if necessary.
850      *
851      * @return an AccessibleJDialog that serves as the
852      * AccessibleContext of this JDialog
853      */

854     public AccessibleContext getAccessibleContext() {
855         if (accessibleContext == null) {
856             accessibleContext = new AccessibleJDialog();
857         }
858         return accessibleContext;
859     }
860
861     /**
862      * This class implements accessibility support for the
863      * <code>JDialog</code> class. It provides an implementation of the
864      * Java Accessibility API appropriate to dialog user-interface
865      * elements.
866      */

867     protected class AccessibleJDialog extends AccessibleAWTDialog {
868         
869         // AccessibleContext methods
870
//
871
/**
872          * Get the accessible name of this object.
873          *
874          * @return the localized name of the object -- can be null if this
875          * object does not have a name
876          */

877         public String JavaDoc getAccessibleName() {
878             if (accessibleName != null) {
879                 return accessibleName;
880             } else {
881                 if (getTitle() == null) {
882                     return super.getAccessibleName();
883                 } else {
884                     return getTitle();
885                 }
886             }
887         }
888
889         /**
890          * Get the state of this object.
891          *
892          * @return an instance of AccessibleStateSet containing the current
893          * state set of the object
894          * @see AccessibleState
895          */

896         public AccessibleStateSet getAccessibleStateSet() {
897             AccessibleStateSet states = super.getAccessibleStateSet();
898
899             if (isResizable()) {
900                 states.add(AccessibleState.RESIZABLE);
901             }
902             if (getFocusOwner() != null) {
903                 states.add(AccessibleState.ACTIVE);
904             }
905             if (isModal()) {
906                 states.add(AccessibleState.MODAL);
907             }
908             return states;
909         }
910
911     } // inner class AccessibleJDialog
912
}
913
Popular Tags