26
27 import java.awt.AWTEvent;
28 import java.awt.BorderLayout;
29 import java.awt.Component;
30 import java.awt.Container;
31 import java.awt.Frame;
32 import java.awt.Graphics;
33 import java.awt.GraphicsConfiguration;
34 import java.awt.HeadlessException;
35 import java.awt.Image;
36 import java.awt.LayoutManager;
37 import java.awt.event.WindowEvent;
38
39 import javax.accessibility.Accessible;
40 import javax.accessibility.AccessibleContext;
41 import javax.accessibility.AccessibleState;
42 import javax.accessibility.AccessibleStateSet;
43
44
45 /**
46 * An extended version of <code>java.awt.Frame</code> that adds support for
47 * the JFC/Swing component architecture.
48 * You can find task-oriented documentation about using <code>JFrame</code>
49 * in <em>The Java Tutorial</em>, in the section
50 * <a
51 href="http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html">How to Make Frames</a>.
52 *
53 * <p>
54 * The <code>JFrame</code> class is slightly incompatible with <code>Frame</code>.
55 * Like all other JFC/Swing top-level containers,
56 * a <code>JFrame</code> contains a <code>JRootPane</code> as its only child.
57 * The <b>content pane</b> provided by the root pane should,
58 * as a rule, contain
59 * all the non-menu components displayed by the <code>JFrame</code>.
60 * This is different from the AWT <code>Frame</code> case.
61 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout}
62 * methods of this class are overridden, so that they delegate calls
63 * to the corresponding methods of the {@code ContentPane}.
64 * For example, you can add a child component to a frame as follows:
65 * <pre>
66 * frame.add(child);
67 * </pre>
68 * And the child will be added to the contentPane.
69 * The content pane will
70 * always be non-null. Attempting to set it to null will cause the JFrame
71 * to throw an exception. The default content pane will have a BorderLayout
72 * manager set on it.
73 * Refer to {@link javax.swing.RootPaneContainer}
74 * for details on adding, removing and setting the <code>LayoutManager</code>
75 * of a <code>JFrame</code>.
76 * <p>
77 * Unlike a <code>Frame</code>, a <code>JFrame</code> has some notion of how to
78 * respond when the user attempts to close the window. The default behavior
79 * is to simply hide the JFrame when the user closes the window. To change the
80 * default behavior, you invoke the method
81 * {@link #setDefaultCloseOperation}.
82 * To make the <code>JFrame</code> behave the same as a <code>Frame</code>
83 * instance, use
84 * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>.
85 * <p>
86 * For more information on content panes
87 * and other features that root panes provide,
88 * see <a
89 href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>.
90 * <p>
91 * In a multi-screen environment, you can create a <code>JFrame</code>
92 * on a different screen device. See {@link java.awt.Frame} for more
93 * information.
94 * <p>
95 * <strong>Warning:</strong> Swing is not thread safe. For more
96 * information see <a
97 * href="package-summary.html#threading">Swing's Threading
98 * Policy</a>.
99 * <p>
100 * <strong>Warning:</strong>
101 * Serialized objects of this class will not be compatible with
102 * future Swing releases. The current serialization support is
103 * appropriate for short term storage or RMI between applications running
104 * the same version of Swing. As of 1.4, support for long term storage
105 * of all JavaBeans™
106 * has been added to the <code>java.beans</code> package.
107 * Please see {@link java.beans.XMLEncoder}.
108 *
109 * @see JRootPane
110 * @see #setDefaultCloseOperation
111 * @see java.awt.event.WindowListener#windowClosing
112 * @see javax.swing.RootPaneContainer
113 *
114 * @beaninfo
115 * attribute: isContainer true
116 * attribute: containerDelegate getContentPane
117 * description: A toplevel window which can be minimized to an icon.
118 *
119 * @author Jeff Dinkins
120 * @author Georges Saab
121 * @author David Kloba
122 * @since 1.2
123 */
124 @SuppressWarnings("serial") // Same-version serialization only
125 public class JFrame extends Frame implements WindowConstants,
126 Accessible,
127 RootPaneContainer,
128 TransferHandler.HasGetTransferHandler
129 {
130 /**
131 * Key into the AppContext, used to check if should provide decorations
132 * by default.
133 */
134 private static final Object defaultLookAndFeelDecoratedKey =
135 new StringBuffer("JFrame.defaultLookAndFeelDecorated");
136
137 private int defaultCloseOperation = HIDE_ON_CLOSE;
138
139 /**
140 * The <code>TransferHandler</code> for this frame.
141 */
142 private TransferHandler transferHandler;
143
144 /**
145 * The <code>JRootPane</code> instance that manages the
146 * <code>contentPane</code>
147 * and optional <code>menuBar</code> for this frame, as well as the
148 * <code>glassPane</code>.
149 *
150 * @see JRootPane
151 * @see RootPaneContainer
152 */
153 protected JRootPane rootPane;
154
155 /**
156 * If true then calls to <code>add</code> and <code>setLayout</code>
157 * will be forwarded to the <code>contentPane</code>. This is initially
158 * false, but is set to true when the <code>JFrame</code> is constructed.
159 *
160 * @see #isRootPaneCheckingEnabled
161 * @see #setRootPaneCheckingEnabled
162 * @see javax.swing.RootPaneContainer
163 */
164 protected boolean rootPaneCheckingEnabled = false;
165
166
167 /**
168 * Constructs a new frame that is initially invisible.
169 * <p>
170 * This constructor sets the component's locale property to the value
171 * returned by <code>JComponent.getDefaultLocale</code>.
172 *
173 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
174 * returns true.
175 * @see java.awt.GraphicsEnvironment#isHeadless
176 * @see Component#setSize
177 * @see Component#setVisible
178 * @see JComponent#getDefaultLocale
179 */
180 public JFrame() throws HeadlessException {
181 super();
182 frameInit();
183 }
184
185 /**
186 * Creates a <code>Frame</code> in the specified
187 * <code>GraphicsConfiguration</code> of
188 * a screen device and a blank title.
189 * <p>
190 * This constructor sets the component's locale property to the value
191 * returned by <code>JComponent.getDefaultLocale</code>.
192 *
193 * @param gc the <code>GraphicsConfiguration</code> that is used
194 * to construct the new <code>Frame</code>;
195 * if <code>gc</code> is <code>null</code>, the system
196 * default <code>GraphicsConfiguration</code> is assumed
197 * @exception IllegalArgumentException if <code>gc</code> is not from
198 * a screen device. This exception is always thrown when
199 * GraphicsEnvironment.isHeadless() returns true.
200 * @see java.awt.GraphicsEnvironment#isHeadless
201 * @see JComponent#getDefaultLocale
202 * @since 1.3
203 */
204 public JFrame(GraphicsConfiguration gc) {
205 super(gc);
206 frameInit();
207 }
208
209 /**
210 * Creates a new, initially invisible <code>Frame</code> with the
211 * specified title.
212 * <p>
213 * This constructor sets the component's locale property to the value
214 * returned by <code>JComponent.getDefaultLocale</code>.
215 *
216 * @param title the title for the frame
217 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
218 * returns true.
219 * @see java.awt.GraphicsEnvironment#isHeadless
220 * @see Component#setSize
221 * @see Component#setVisible
222 * @see JComponent#getDefaultLocale
223 */
224 public JFrame(String title) throws HeadlessException {
225 super(title);
226 frameInit();
227 }
228
229 /**
230 * Creates a <code>JFrame</code> with the specified title and the
231 * specified <code>GraphicsConfiguration</code> of a screen device.
232 * <p>
233 * This constructor sets the component's locale property to the value
234 * returned by <code>JComponent.getDefaultLocale</code>.
235 *
236 * @param title the title to be displayed in the
237 * frame's border. A <code>null</code> value is treated as
238 * an empty string, "".
239 * @param gc the <code>GraphicsConfiguration</code> that is used
240 * to construct the new <code>JFrame</code> with;
241 * if <code>gc</code> is <code>null</code>, the system
242 * default <code>GraphicsConfiguration</code> is assumed
243 * @exception IllegalArgumentException if <code>gc</code> is not from
244 * a screen device. This exception is always thrown when
245 * GraphicsEnvironment.isHeadless() returns true.
246 * @see java.awt.GraphicsEnvironment#isHeadless
247 * @see JComponent#getDefaultLocale
248 * @since 1.3
249 */
250 public JFrame(String title, GraphicsConfiguration gc) {
251 super(title, gc);
252 frameInit();
253 }
254
255 /** Called by the constructors to init the <code>JFrame</code> properly. */
256 protected void frameInit() {
257 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
258 setLocale( JComponent.getDefaultLocale() );
259 setRootPane(createRootPane());
260 setBackground(UIManager.getColor("control"));
261 setRootPaneCheckingEnabled(true);
262 if (JFrame.isDefaultLookAndFeelDecorated()) {
263 boolean supportsWindowDecorations =
264 UIManager.getLookAndFeel().getSupportsWindowDecorations();
265 if (supportsWindowDecorations) {
266 setUndecorated(true);
267 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
268 }
269 }
270 sun.awt.SunToolkit.checkAndSetPolicy(this);
271 }
272
273 /**
274 * Called by the constructor methods to create the default
275 * <code>rootPane</code>.
276 *
277 * @return a new {@code JRootPane}
278 */
279 protected JRootPane createRootPane() {
280 JRootPane rp = new JRootPane();
281 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
282 // is NO reason for the RootPane not to be opaque. For painting to
283 // work the contentPane must be opaque, therefor the RootPane can
284 // also be opaque.
285 rp.setOpaque(true);
286 return rp;
287 }
288
289 /**
290 * Processes window events occurring on this component.
291 * Hides the window or disposes of it, as specified by the setting
292 * of the <code>defaultCloseOperation</code> property.
293 *
294 * @param e the window event
295 * @see #setDefaultCloseOperation
296 * @see java.awt.Window#processWindowEvent
297 */
298 protected void processWindowEvent(final WindowEvent e) {
299 super.processWindowEvent(e);
300
301 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
302 switch (defaultCloseOperation) {
303 case HIDE_ON_CLOSE:
304 setVisible(false);
305 break;
306 case DISPOSE_ON_CLOSE:
307 dispose();
308 break;
309 case EXIT_ON_CLOSE:
310 // This needs to match the checkExit call in
311 // setDefaultCloseOperation
312 System.exit(0);
313 break;
314 case DO_NOTHING_ON_CLOSE:
315 default:
316 }
317 }
318 }
319
320 /**
321 * Sets the operation that will happen by default when
322 * the user initiates a "close" on this frame.
323 * You must specify one of the following choices:
324 * <br><br>
325 * <ul>
326 * <li><code>DO_NOTHING_ON_CLOSE</code>
327 * (defined in <code>WindowConstants</code>):
328 * Don't do anything; require the
329 * program to handle the operation in the <code>windowClosing</code>
330 * method of a registered <code>WindowListener</code> object.
331 *
332 * <li><code>HIDE_ON_CLOSE</code>
333 * (defined in <code>WindowConstants</code>):
334 * Automatically hide the frame after
335 * invoking any registered <code>WindowListener</code>
336 * objects.
337 *
338 * <li><code>DISPOSE_ON_CLOSE</code>
339 * (defined in <code>WindowConstants</code>):
340 * Automatically hide and dispose the
341 * frame after invoking any registered <code>WindowListener</code>
342 * objects.
343 *
344 * <li><code>EXIT_ON_CLOSE</code>
345 * (defined in <code>WindowConstants</code>):
346 * Exit the application using the <code>System</code>
347 * <code>exit</code> method. Use this only in applications.
348 * </ul>
349 * <p>
350 * The value is set to <code>HIDE_ON_CLOSE</code> by default. Changes
351 * to the value of this property cause the firing of a property
352 * change event, with property name "defaultCloseOperation".
353 * <p>
354 * <b>Note</b>: When the last displayable window within the
355 * Java virtual machine (VM) is disposed of, the VM may
356 * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
357 * AWT Threading Issues</a> for more information.
358 *
359 * @param operation the operation which should be performed when the
360 * user closes the frame
361 * @exception IllegalArgumentException if defaultCloseOperation value
362 * isn't one of the above valid values
363 * @see #addWindowListener
364 * @see #getDefaultCloseOperation
365 * @see WindowConstants
366 * @throws SecurityException
367 * if <code>EXIT_ON_CLOSE</code> has been specified and the
368 * <code>SecurityManager</code> will
369 * not allow the caller to invoke <code>System.exit</code>
370 * @see java.lang.Runtime#exit(int)
371 *
372 * @beaninfo
373 * preferred: true
374 * bound: true
375 * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE
376 * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
377 * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
378 * EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE
379 * description: The frame's default close operation.
380 */
381 public void setDefaultCloseOperation(int operation) {
382 if (operation != DO_NOTHING_ON_CLOSE &&
383 operation != HIDE_ON_CLOSE &&
384 operation != DISPOSE_ON_CLOSE &&
385 operation != EXIT_ON_CLOSE) {
386 throw new IllegalArgumentException("defaultCloseOperation must be"
387 + " one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE,"
388 + " DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
389 }
437 * @param newHandler the new {@code TransferHandler}
438 *
439 * @see TransferHandler
440 * @see #getTransferHandler
441 * @see java.awt.Component#setDropTarget
442 * @since 1.6
443 *
444 * @beaninfo
445 * bound: true
446 * hidden: true
447 * description: Mechanism for transfer of data into the component
448 */
449 public void setTransferHandler(TransferHandler newHandler) {
450 TransferHandler oldHandler = transferHandler;
451 transferHandler = newHandler;
452 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler);
453 firePropertyChange("transferHandler", oldHandler, newHandler);
454 }
455
456 /**
457 * Gets the <code>transferHandler</code> property.
458 *
459 * @return the value of the <code>transferHandler</code> property
460 *
461 * @see TransferHandler
462 * @see #setTransferHandler
463 * @since 1.6
464 */
465 public TransferHandler getTransferHandler() {
466 return transferHandler;
467 }
468
469 /**
470 * Just calls <code>paint(g)</code>. This method was overridden to
471 * prevent an unnecessary call to clear the background.
472 *
473 * @param g the Graphics context in which to paint
474 */
475 public void update(Graphics g) {
476 paint(g);
477 }
478
479 /**
480 * Sets the menubar for this frame.
481 * @param menubar the menubar being placed in the frame
482 *
483 * @see #getJMenuBar
484 *
485 * @beaninfo
486 * hidden: true
487 * description: The menubar for accessing pulldown menus from this frame.
488 */
489 public void setJMenuBar(final JMenuBar menubar) {
490 getRootPane().setJMenuBar(menubar);
491 }
492
493 /**
494 * Returns the menubar set on this frame.
495 * @return the menubar for this frame
496 *
497 * @see #setJMenuBar
498 */
499 public JMenuBar getJMenuBar() {
500 return getRootPane().getJMenuBar();
501 }
502
503 /**
504 * Returns whether calls to <code>add</code> and
505 * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
506 *
507 * @return true if <code>add</code> and <code>setLayout</code>
508 * are forwarded; false otherwise
509 *
510 * @see #addImpl
511 * @see #setLayout
512 * @see #setRootPaneCheckingEnabled
513 * @see javax.swing.RootPaneContainer
514 */
515 protected boolean isRootPaneCheckingEnabled() {
516 return rootPaneCheckingEnabled;
517 }
518
519
520 /**
521 * Sets whether calls to <code>add</code> and
522 * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
523 *
524 * @param enabled true if <code>add</code> and <code>setLayout</code>
525 * are forwarded, false if they should operate directly on the
526 * <code>JFrame</code>.
527 *
528 * @see #addImpl
529 * @see #setLayout
530 * @see #isRootPaneCheckingEnabled
531 * @see javax.swing.RootPaneContainer
532 * @beaninfo
533 * hidden: true
534 * description: Whether the add and setLayout methods are forwarded
535 */
536 protected void setRootPaneCheckingEnabled(boolean enabled) {
537 rootPaneCheckingEnabled = enabled;
538 }
539
540
541 /**
542 * Adds the specified child <code>Component</code>.
543 * This method is overridden to conditionally forward calls to the
544 * <code>contentPane</code>.
545 * By default, children are added to the <code>contentPane</code> instead
546 * of the frame, refer to {@link javax.swing.RootPaneContainer} for
547 * details.
548 *
549 * @param comp the component to be enhanced
550 * @param constraints the constraints to be respected
551 * @param index the index
552 * @exception IllegalArgumentException if <code>index</code> is invalid
553 * @exception IllegalArgumentException if adding the container's parent
554 * to itself
555 * @exception IllegalArgumentException if adding a window to a container
556 *
557 * @see #setRootPaneCheckingEnabled
558 * @see javax.swing.RootPaneContainer
559 */
560 protected void addImpl(Component comp, Object constraints, int index)
561 {
562 if(isRootPaneCheckingEnabled()) {
563 getContentPane().add(comp, constraints, index);
564 }
565 else {
566 super.addImpl(comp, constraints, index);
567 }
568 }
569
570 /**
571 * Removes the specified component from the container. If
572 * <code>comp</code> is not the <code>rootPane</code>, this will forward
573 * the call to the <code>contentPane</code>. This will do nothing if
574 * <code>comp</code> is not a child of the <code>JFrame</code> or
575 * <code>contentPane</code>.
576 *
577 * @param comp the component to be removed
578 * @throws NullPointerException if <code>comp</code> is null
579 * @see #add
580 * @see javax.swing.RootPaneContainer
581 */
582 public void remove(Component comp) {
583 if (comp == rootPane) {
584 super.remove(comp);
585 } else {
586 getContentPane().remove(comp);
587 }
588 }
589
590
591 /**
592 * Sets the <code>LayoutManager</code>.
593 * Overridden to conditionally forward the call to the
594 * <code>contentPane</code>.
595 * Refer to {@link javax.swing.RootPaneContainer} for
596 * more information.
597 *
598 * @param manager the <code>LayoutManager</code>
599 * @see #setRootPaneCheckingEnabled
600 * @see javax.swing.RootPaneContainer
601 */
602 public void setLayout(LayoutManager manager) {
603 if(isRootPaneCheckingEnabled()) {
604 getContentPane().setLayout(manager);
605 }
606 else {
607 super.setLayout(manager);
608 }
609 }
610
611
612 /**
613 * Returns the <code>rootPane</code> object for this frame.
614 * @return the <code>rootPane</code> property
615 *
616 * @see #setRootPane
617 * @see RootPaneContainer#getRootPane
618 */
619 public JRootPane getRootPane() {
620 return rootPane;
621 }
622
623
624 /**
625 * Sets the <code>rootPane</code> property.
626 * This method is called by the constructor.
627 * @param root the <code>rootPane</code> object for this frame
628 *
629 * @see #getRootPane
630 *
631 * @beaninfo
632 * hidden: true
633 * description: the RootPane object for this frame.
634 */
635 protected void setRootPane(JRootPane root)
636 {
637 if(rootPane != null) {
638 remove(rootPane);
639 }
640 rootPane = root;
641 if(rootPane != null) {
642 boolean checkingEnabled = isRootPaneCheckingEnabled();
643 try {
644 setRootPaneCheckingEnabled(false);
645 add(rootPane, BorderLayout.CENTER);
646 }
647 finally {
648 setRootPaneCheckingEnabled(checkingEnabled);
649 }
650 }
651 }
652
653 /**
654 * {@inheritDoc}
655 */
656 public void setIconImage(Image image) {
657 super.setIconImage(image);
658 }
659
660 /**
661 * Returns the <code>contentPane</code> object for this frame.
662 * @return the <code>contentPane</code> property
663 *
664 * @see #setContentPane
665 * @see RootPaneContainer#getContentPane
666 */
667 public Container getContentPane() {
668 return getRootPane().getContentPane();
669 }
670
671 /**
672 * Sets the <code>contentPane</code> property.
673 * This method is called by the constructor.
674 * <p>
675 * Swing's painting architecture requires an opaque <code>JComponent</code>
676 * in the containment hierarchy. This is typically provided by the
677 * content pane. If you replace the content pane it is recommended you
678 * replace it with an opaque <code>JComponent</code>.
679 *
680 * @param contentPane the <code>contentPane</code> object for this frame
681 *
682 * @exception java.awt.IllegalComponentStateException (a runtime
683 * exception) if the content pane parameter is <code>null</code>
684 * @see #getContentPane
685 * @see RootPaneContainer#setContentPane
686 * @see JRootPane
687 *
688 * @beaninfo
689 * hidden: true
690 * description: The client area of the frame where child
691 * components are normally inserted.
692 */
693 public void setContentPane(Container contentPane) {
694 getRootPane().setContentPane(contentPane);
695 }
696
697 /**
698 * Returns the <code>layeredPane</code> object for this frame.
699 * @return the <code>layeredPane</code> property
700 *
701 * @see #setLayeredPane
702 * @see RootPaneContainer#getLayeredPane
703 */
704 public JLayeredPane getLayeredPane() {
705 return getRootPane().getLayeredPane();
706 }
707
708 /**
709 * Sets the <code>layeredPane</code> property.
710 * This method is called by the constructor.
711 * @param layeredPane the <code>layeredPane</code> object for this frame
712 *
713 * @exception java.awt.IllegalComponentStateException (a runtime
714 * exception) if the layered pane parameter is <code>null</code>
715 * @see #getLayeredPane
716 * @see RootPaneContainer#setLayeredPane
717 *
718 * @beaninfo
719 * hidden: true
720 * description: The pane that holds the various frame layers.
721 */
722 public void setLayeredPane(JLayeredPane layeredPane) {
723 getRootPane().setLayeredPane(layeredPane);
724 }
725
726 /**
727 * Returns the <code>glassPane</code> object for this frame.
728 * @return the <code>glassPane</code> property
729 *
730 * @see #setGlassPane
731 * @see RootPaneContainer#getGlassPane
732 */
733 public Component getGlassPane() {
734 return getRootPane().getGlassPane();
735 }
736
737 /**
738 * Sets the <code>glassPane</code> property.
739 * This method is called by the constructor.
740 * @param glassPane the <code>glassPane</code> object for this frame
741 *
742 * @see #getGlassPane
743 * @see RootPaneContainer#setGlassPane
744 *
745 * @beaninfo
746 * hidden: true
747 * description: A transparent pane used for menu rendering.
748 */
749 public void setGlassPane(Component glassPane) {
750 getRootPane().setGlassPane(glassPane);
751 }
752
753 /**
754 * {@inheritDoc}
755 *
756 * @since 1.6
757 */
758 public Graphics getGraphics() {
759 JComponent.getGraphicsInvoked(this);
760 return super.getGraphics();
761 }
762
763 /**
764 * Repaints the specified rectangle of this component within
765 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code>
766 * for details on how the repaint is handled.
767 *
768 * @param time maximum time in milliseconds before update
769 * @param x the <i>x</i> coordinate
770 * @param y the <i>y</i> coordinate
771 * @param width the width
772 * @param height the height
773 * @see RepaintManager
774 * @since 1.6
775 */
776 public void repaint(long time, int x, int y, int width, int height) {
777 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
778 RepaintManager.currentManager(this).addDirtyRegion(
779 this, x, y, width, height);
780 }
781 else {
782 super.repaint(time, x, y, width, height);
783 }
784 }
785
786 /**
787 * Provides a hint as to whether or not newly created <code>JFrame</code>s
788 * should have their Window decorations (such as borders, widgets to
789 * close the window, title...) provided by the current look
790 * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
791 * the current <code>LookAndFeel</code> supports providing window
792 * decorations, and the current window manager supports undecorated
793 * windows, then newly created <code>JFrame</code>s will have their
794 * Window decorations provided by the current <code>LookAndFeel</code>.
795 * Otherwise, newly created <code>JFrame</code>s will have their
796 * Window decorations provided by the current window manager.
797 * <p>
798 * You can get the same effect on a single JFrame by doing the following:
799 * <pre>
800 * JFrame frame = new JFrame();
801 * frame.setUndecorated(true);
802 * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
803 * </pre>
804 *
805 * @param defaultLookAndFeelDecorated A hint as to whether or not current
806 * look and feel should provide window decorations
807 * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
808 * @since 1.4
809 */
810 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
811 if (defaultLookAndFeelDecorated) {
812 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
813 } else {
814 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
815 }
816 }
817
818
819 /**
820 * Returns true if newly created <code>JFrame</code>s should have their
821 * Window decorations provided by the current look and feel. This is only
822 * a hint, as certain look and feels may not support this feature.
823 *
824 * @return true if look and feel should provide Window decorations.
825 * @since 1.4
826 */
827 public static boolean isDefaultLookAndFeelDecorated() {
828 Boolean defaultLookAndFeelDecorated =
829 (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
830 if (defaultLookAndFeelDecorated == null) {
831 defaultLookAndFeelDecorated = Boolean.FALSE;
832 }
833 return defaultLookAndFeelDecorated.booleanValue();
834 }
835
836 /**
837 * Returns a string representation of this <code>JFrame</code>.
838 * This method
839 * is intended to be used only for debugging purposes, and the
840 * content and format of the returned string may vary between
841 * implementations. The returned string may be empty but may not
842 * be <code>null</code>.
843 *
844 * @return a string representation of this <code>JFrame</code>
845 */
846 protected String paramString() {
847 String defaultCloseOperationString;
848 if (defaultCloseOperation == HIDE_ON_CLOSE) {
849 defaultCloseOperationString = "HIDE_ON_CLOSE";
850 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
851 defaultCloseOperationString = "DISPOSE_ON_CLOSE";
852 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
853 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
854 } else if (defaultCloseOperation == EXIT_ON_CLOSE) {
855 defaultCloseOperationString = "EXIT_ON_CLOSE";
856 } else defaultCloseOperationString = "";
857 String rootPaneString = (rootPane != null ?
858 rootPane.toString() : "");
859 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
860 "true" : "false");
861
862 return super.paramString() +
863 ",defaultCloseOperation=" + defaultCloseOperationString +
864 ",rootPane=" + rootPaneString +
877 protected AccessibleContext accessibleContext = null;
878
879 /**
880 * Gets the AccessibleContext associated with this JFrame.
881 * For JFrames, the AccessibleContext takes the form of an
882 * AccessibleJFrame.
883 * A new AccessibleJFrame instance is created if necessary.
884 *
885 * @return an AccessibleJFrame that serves as the
886 * AccessibleContext of this JFrame
887 */
888 public AccessibleContext getAccessibleContext() {
889 if (accessibleContext == null) {
890 accessibleContext = new AccessibleJFrame();
891 }
892 return accessibleContext;
893 }
894
895 /**
896 * This class implements accessibility support for the
897 * <code>JFrame</code> class. It provides an implementation of the
898 * Java Accessibility API appropriate to frame user-interface
899 * elements.
900 */
901 protected class AccessibleJFrame extends AccessibleAWTFrame {
902
903 // AccessibleContext methods
904 /**
905 * Get the accessible name of this object.
906 *
907 * @return the localized name of the object -- can be null if this
908 * object does not have a name
909 */
910 public String getAccessibleName() {
911 if (accessibleName != null) {
912 return accessibleName;
913 } else {
914 if (getTitle() == null) {
915 return super.getAccessibleName();
916 } else {
917 return getTitle();
|
26
27 import java.awt.AWTEvent;
28 import java.awt.BorderLayout;
29 import java.awt.Component;
30 import java.awt.Container;
31 import java.awt.Frame;
32 import java.awt.Graphics;
33 import java.awt.GraphicsConfiguration;
34 import java.awt.HeadlessException;
35 import java.awt.Image;
36 import java.awt.LayoutManager;
37 import java.awt.event.WindowEvent;
38
39 import javax.accessibility.Accessible;
40 import javax.accessibility.AccessibleContext;
41 import javax.accessibility.AccessibleState;
42 import javax.accessibility.AccessibleStateSet;
43
44
45 /**
46 * An extended version of {@code java.awt.Frame} that adds support for
47 * the JFC/Swing component architecture.
48 * You can find task-oriented documentation about using {@code JFrame}
49 * in <em>The Java Tutorial</em>, in the section
50 * <a
51 href="http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html">How to Make Frames</a>.
52 *
53 * <p>
54 * The {@code JFrame} class is slightly incompatible with {@code Frame}.
55 * Like all other JFC/Swing top-level containers,
56 * a {@code JFrame} contains a {@code JRootPane} as its only child.
57 * The <b>content pane</b> provided by the root pane should,
58 * as a rule, contain
59 * all the non-menu components displayed by the {@code JFrame}.
60 * This is different from the AWT {@code Frame} case.
61 * As a convenience, the {@code add}, {@code remove}, and {@code setLayout}
62 * methods of this class are overridden, so that they delegate calls
63 * to the corresponding methods of the {@code ContentPane}.
64 * For example, you can add a child component to a frame as follows:
65 * <pre>
66 * frame.add(child);
67 * </pre>
68 * And the child will be added to the contentPane.
69 * The content pane will
70 * always be non-null. Attempting to set it to null will cause the JFrame
71 * to throw an exception. The default content pane will have a BorderLayout
72 * manager set on it.
73 * Refer to {@link javax.swing.RootPaneContainer}
74 * for details on adding, removing and setting the {@code LayoutManager}
75 * of a {@code JFrame}.
76 * <p>
77 * Unlike a {@code Frame}, a {@code JFrame} has some notion of how to
78 * respond when the user attempts to close the window. The default behavior
79 * is to simply hide the JFrame when the user closes the window. To change the
80 * default behavior, you invoke the method
81 * {@link #setDefaultCloseOperation}.
82 * To make the {@code JFrame} behave the same as a {@code Frame}
83 * instance, use
84 * {@code setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)}.
85 * <p>
86 * For more information on content panes
87 * and other features that root panes provide,
88 * see <a
89 href="http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>.
90 * <p>
91 * In a multi-screen environment, you can create a {@code JFrame}
92 * on a different screen device. See {@link java.awt.Frame} for more
93 * information.
94 * <p>
95 * <strong>Warning:</strong> Swing is not thread safe. For more
96 * information see <a
97 * href="package-summary.html#threading">Swing's Threading
98 * Policy</a>.
99 * <p>
100 * <strong>Warning:</strong>
101 * Serialized objects of this class will not be compatible with
102 * future Swing releases. The current serialization support is
103 * appropriate for short term storage or RMI between applications running
104 * the same version of Swing. As of 1.4, support for long term storage
105 * of all JavaBeans™
106 * has been added to the {@code java.beans} package.
107 * Please see {@link java.beans.XMLEncoder}.
108 *
109 * @see JRootPane
110 * @see #setDefaultCloseOperation
111 * @see java.awt.event.WindowListener#windowClosing
112 * @see javax.swing.RootPaneContainer
113 *
114 * @beaninfo
115 * attribute: isContainer true
116 * attribute: containerDelegate getContentPane
117 * description: A toplevel window which can be minimized to an icon.
118 *
119 * @author Jeff Dinkins
120 * @author Georges Saab
121 * @author David Kloba
122 * @since 1.2
123 */
124 @SuppressWarnings("serial") // Same-version serialization only
125 public class JFrame extends Frame implements WindowConstants,
126 Accessible,
127 RootPaneContainer,
128 TransferHandler.HasGetTransferHandler
129 {
130 /**
131 * Key into the AppContext, used to check if should provide decorations
132 * by default.
133 */
134 private static final Object defaultLookAndFeelDecoratedKey =
135 new StringBuffer("JFrame.defaultLookAndFeelDecorated");
136
137 private int defaultCloseOperation = HIDE_ON_CLOSE;
138
139 /**
140 * The {@code TransferHandler} for this frame.
141 */
142 private TransferHandler transferHandler;
143
144 /**
145 * The {@code JRootPane} instance that manages the
146 * {@code contentPane}
147 * and optional {@code menuBar} for this frame, as well as the
148 * {@code glassPane}.
149 *
150 * @see JRootPane
151 * @see RootPaneContainer
152 */
153 protected JRootPane rootPane;
154
155 /**
156 * If true then calls to {@code add} and {@code setLayout}
157 * will be forwarded to the {@code contentPane}. This is initially
158 * false, but is set to true when the {@code JFrame} is constructed.
159 *
160 * @see #isRootPaneCheckingEnabled
161 * @see #setRootPaneCheckingEnabled
162 * @see javax.swing.RootPaneContainer
163 */
164 protected boolean rootPaneCheckingEnabled = false;
165
166
167 /**
168 * Constructs a new frame that is initially invisible.
169 * <p>
170 * This constructor sets the component's locale property to the value
171 * returned by {@code JComponent.getDefaultLocale}.
172 *
173 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
174 * returns true.
175 * @see java.awt.GraphicsEnvironment#isHeadless
176 * @see Component#setSize
177 * @see Component#setVisible
178 * @see JComponent#getDefaultLocale
179 */
180 public JFrame() throws HeadlessException {
181 super();
182 frameInit();
183 }
184
185 /**
186 * Creates a {@code Frame} in the specified
187 * {@code GraphicsConfiguration} of
188 * a screen device and a blank title.
189 * <p>
190 * This constructor sets the component's locale property to the value
191 * returned by {@code JComponent.getDefaultLocale}.
192 *
193 * @param gc the {@code GraphicsConfiguration} that is used
194 * to construct the new {@code Frame};
195 * if {@code gc} is {@code null}, the system
196 * default {@code GraphicsConfiguration} is assumed
197 * @exception IllegalArgumentException if {@code gc} is not from
198 * a screen device. This exception is always thrown when
199 * GraphicsEnvironment.isHeadless() returns true.
200 * @see java.awt.GraphicsEnvironment#isHeadless
201 * @see JComponent#getDefaultLocale
202 * @since 1.3
203 */
204 public JFrame(GraphicsConfiguration gc) {
205 super(gc);
206 frameInit();
207 }
208
209 /**
210 * Creates a new, initially invisible {@code Frame} with the
211 * specified title.
212 * <p>
213 * This constructor sets the component's locale property to the value
214 * returned by {@code JComponent.getDefaultLocale}.
215 *
216 * @param title the title for the frame
217 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
218 * returns true.
219 * @see java.awt.GraphicsEnvironment#isHeadless
220 * @see Component#setSize
221 * @see Component#setVisible
222 * @see JComponent#getDefaultLocale
223 */
224 public JFrame(String title) throws HeadlessException {
225 super(title);
226 frameInit();
227 }
228
229 /**
230 * Creates a {@code JFrame} with the specified title and the
231 * specified {@code GraphicsConfiguration} of a screen device.
232 * <p>
233 * This constructor sets the component's locale property to the value
234 * returned by {@code JComponent.getDefaultLocale}.
235 *
236 * @param title the title to be displayed in the
237 * frame's border. A {@code null} value is treated as
238 * an empty string, "".
239 * @param gc the {@code GraphicsConfiguration} that is used
240 * to construct the new {@code JFrame} with;
241 * if {@code gc} is {@code null}, the system
242 * default {@code GraphicsConfiguration} is assumed
243 * @exception IllegalArgumentException if {@code gc} is not from
244 * a screen device. This exception is always thrown when
245 * GraphicsEnvironment.isHeadless() returns true.
246 * @see java.awt.GraphicsEnvironment#isHeadless
247 * @see JComponent#getDefaultLocale
248 * @since 1.3
249 */
250 public JFrame(String title, GraphicsConfiguration gc) {
251 super(title, gc);
252 frameInit();
253 }
254
255 /** Called by the constructors to init the {@code JFrame} properly. */
256 protected void frameInit() {
257 enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
258 setLocale( JComponent.getDefaultLocale() );
259 setRootPane(createRootPane());
260 setBackground(UIManager.getColor("control"));
261 setRootPaneCheckingEnabled(true);
262 if (JFrame.isDefaultLookAndFeelDecorated()) {
263 boolean supportsWindowDecorations =
264 UIManager.getLookAndFeel().getSupportsWindowDecorations();
265 if (supportsWindowDecorations) {
266 setUndecorated(true);
267 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
268 }
269 }
270 sun.awt.SunToolkit.checkAndSetPolicy(this);
271 }
272
273 /**
274 * Called by the constructor methods to create the default
275 * {@code rootPane}.
276 *
277 * @return a new {@code JRootPane}
278 */
279 protected JRootPane createRootPane() {
280 JRootPane rp = new JRootPane();
281 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
282 // is NO reason for the RootPane not to be opaque. For painting to
283 // work the contentPane must be opaque, therefor the RootPane can
284 // also be opaque.
285 rp.setOpaque(true);
286 return rp;
287 }
288
289 /**
290 * Processes window events occurring on this component.
291 * Hides the window or disposes of it, as specified by the setting
292 * of the {@code defaultCloseOperation} property.
293 *
294 * @param e the window event
295 * @see #setDefaultCloseOperation
296 * @see java.awt.Window#processWindowEvent
297 */
298 protected void processWindowEvent(final WindowEvent e) {
299 super.processWindowEvent(e);
300
301 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
302 switch (defaultCloseOperation) {
303 case HIDE_ON_CLOSE:
304 setVisible(false);
305 break;
306 case DISPOSE_ON_CLOSE:
307 dispose();
308 break;
309 case EXIT_ON_CLOSE:
310 // This needs to match the checkExit call in
311 // setDefaultCloseOperation
312 System.exit(0);
313 break;
314 case DO_NOTHING_ON_CLOSE:
315 default:
316 }
317 }
318 }
319
320 /**
321 * Sets the operation that will happen by default when
322 * the user initiates a "close" on this frame.
323 * You must specify one of the following choices:
324 * <br><br>
325 * <ul>
326 * <li>{@code DO_NOTHING_ON_CLOSE}
327 * (defined in {@code WindowConstants}):
328 * Don't do anything; require the
329 * program to handle the operation in the {@code windowClosing}
330 * method of a registered {@code WindowListener} object.
331 *
332 * <li>{@code HIDE_ON_CLOSE}
333 * (defined in {@code WindowConstants}):
334 * Automatically hide the frame after
335 * invoking any registered {@code WindowListener}
336 * objects.
337 *
338 * <li>{@code DISPOSE_ON_CLOSE}
339 * (defined in {@code WindowConstants}):
340 * Automatically hide and dispose the
341 * frame after invoking any registered {@code WindowListener}
342 * objects.
343 *
344 * <li>{@code EXIT_ON_CLOSE}
345 * (defined in {@code WindowConstants}):
346 * Exit the application using the {@code System}
347 * {@code exit} method. Use this only in applications.
348 * </ul>
349 * <p>
350 * The value is set to {@code HIDE_ON_CLOSE} by default. Changes
351 * to the value of this property cause the firing of a property
352 * change event, with property name "defaultCloseOperation".
353 * <p>
354 * <b>Note</b>: When the last displayable window within the
355 * Java virtual machine (VM) is disposed of, the VM may
356 * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
357 * AWT Threading Issues</a> for more information.
358 *
359 * @param operation the operation which should be performed when the
360 * user closes the frame
361 * @exception IllegalArgumentException if defaultCloseOperation value
362 * isn't one of the above valid values
363 * @see #addWindowListener
364 * @see #getDefaultCloseOperation
365 * @see WindowConstants
366 * @throws SecurityException
367 * if {@code EXIT_ON_CLOSE} has been specified and the
368 * {@code SecurityManager} will
369 * not allow the caller to invoke {@code System.exit}
370 * @see java.lang.Runtime#exit(int)
371 *
372 * @beaninfo
373 * preferred: true
374 * bound: true
375 * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE
376 * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
377 * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
378 * EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE
379 * description: The frame's default close operation.
380 */
381 public void setDefaultCloseOperation(int operation) {
382 if (operation != DO_NOTHING_ON_CLOSE &&
383 operation != HIDE_ON_CLOSE &&
384 operation != DISPOSE_ON_CLOSE &&
385 operation != EXIT_ON_CLOSE) {
386 throw new IllegalArgumentException("defaultCloseOperation must be"
387 + " one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE,"
388 + " DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
389 }
437 * @param newHandler the new {@code TransferHandler}
438 *
439 * @see TransferHandler
440 * @see #getTransferHandler
441 * @see java.awt.Component#setDropTarget
442 * @since 1.6
443 *
444 * @beaninfo
445 * bound: true
446 * hidden: true
447 * description: Mechanism for transfer of data into the component
448 */
449 public void setTransferHandler(TransferHandler newHandler) {
450 TransferHandler oldHandler = transferHandler;
451 transferHandler = newHandler;
452 SwingUtilities.installSwingDropTargetAsNecessary(this, transferHandler);
453 firePropertyChange("transferHandler", oldHandler, newHandler);
454 }
455
456 /**
457 * Gets the {@code transferHandler} property.
458 *
459 * @return the value of the {@code transferHandler} property
460 *
461 * @see TransferHandler
462 * @see #setTransferHandler
463 * @since 1.6
464 */
465 public TransferHandler getTransferHandler() {
466 return transferHandler;
467 }
468
469 /**
470 * Just calls {@code paint(g)}. This method was overridden to
471 * prevent an unnecessary call to clear the background.
472 *
473 * @param g the Graphics context in which to paint
474 */
475 public void update(Graphics g) {
476 paint(g);
477 }
478
479 /**
480 * Sets the menubar for this frame.
481 * @param menubar the menubar being placed in the frame
482 *
483 * @see #getJMenuBar
484 *
485 * @beaninfo
486 * hidden: true
487 * description: The menubar for accessing pulldown menus from this frame.
488 */
489 public void setJMenuBar(final JMenuBar menubar) {
490 getRootPane().setJMenuBar(menubar);
491 }
492
493 /**
494 * Returns the menubar set on this frame.
495 * @return the menubar for this frame
496 *
497 * @see #setJMenuBar
498 */
499 public JMenuBar getJMenuBar() {
500 return getRootPane().getJMenuBar();
501 }
502
503 /**
504 * Returns whether calls to {@code add} and
505 * {@code setLayout} are forwarded to the {@code contentPane}.
506 *
507 * @return true if {@code add} and {@code setLayout}
508 * are forwarded; false otherwise
509 *
510 * @see #addImpl
511 * @see #setLayout
512 * @see #setRootPaneCheckingEnabled
513 * @see javax.swing.RootPaneContainer
514 */
515 protected boolean isRootPaneCheckingEnabled() {
516 return rootPaneCheckingEnabled;
517 }
518
519
520 /**
521 * Sets whether calls to {@code add} and
522 * {@code setLayout} are forwarded to the {@code contentPane}.
523 *
524 * @param enabled true if {@code add} and {@code setLayout}
525 * are forwarded, false if they should operate directly on the
526 * {@code JFrame}.
527 *
528 * @see #addImpl
529 * @see #setLayout
530 * @see #isRootPaneCheckingEnabled
531 * @see javax.swing.RootPaneContainer
532 * @beaninfo
533 * hidden: true
534 * description: Whether the add and setLayout methods are forwarded
535 */
536 protected void setRootPaneCheckingEnabled(boolean enabled) {
537 rootPaneCheckingEnabled = enabled;
538 }
539
540
541 /**
542 * Adds the specified child {@code Component}.
543 * This method is overridden to conditionally forward calls to the
544 * {@code contentPane}.
545 * By default, children are added to the {@code contentPane} instead
546 * of the frame, refer to {@link javax.swing.RootPaneContainer} for
547 * details.
548 *
549 * @param comp the component to be enhanced
550 * @param constraints the constraints to be respected
551 * @param index the index
552 * @exception IllegalArgumentException if {@code index} is invalid
553 * @exception IllegalArgumentException if adding the container's parent
554 * to itself
555 * @exception IllegalArgumentException if adding a window to a container
556 *
557 * @see #setRootPaneCheckingEnabled
558 * @see javax.swing.RootPaneContainer
559 */
560 protected void addImpl(Component comp, Object constraints, int index)
561 {
562 if(isRootPaneCheckingEnabled()) {
563 getContentPane().add(comp, constraints, index);
564 }
565 else {
566 super.addImpl(comp, constraints, index);
567 }
568 }
569
570 /**
571 * Removes the specified component from the container. If
572 * {@code comp} is not the {@code rootPane}, this will forward
573 * the call to the {@code contentPane}. This will do nothing if
574 * {@code comp} is not a child of the {@code JFrame} or
575 * {@code contentPane}.
576 *
577 * @param comp the component to be removed
578 * @throws NullPointerException if {@code comp} is null
579 * @see #add
580 * @see javax.swing.RootPaneContainer
581 */
582 public void remove(Component comp) {
583 if (comp == rootPane) {
584 super.remove(comp);
585 } else {
586 getContentPane().remove(comp);
587 }
588 }
589
590
591 /**
592 * Sets the {@code LayoutManager}.
593 * Overridden to conditionally forward the call to the
594 * {@code contentPane}.
595 * Refer to {@link javax.swing.RootPaneContainer} for
596 * more information.
597 *
598 * @param manager the {@code LayoutManager}
599 * @see #setRootPaneCheckingEnabled
600 * @see javax.swing.RootPaneContainer
601 */
602 public void setLayout(LayoutManager manager) {
603 if(isRootPaneCheckingEnabled()) {
604 getContentPane().setLayout(manager);
605 }
606 else {
607 super.setLayout(manager);
608 }
609 }
610
611
612 /**
613 * Returns the {@code rootPane} object for this frame.
614 * @return the {@code rootPane} property
615 *
616 * @see #setRootPane
617 * @see RootPaneContainer#getRootPane
618 */
619 public JRootPane getRootPane() {
620 return rootPane;
621 }
622
623
624 /**
625 * Sets the {@code rootPane} property.
626 * This method is called by the constructor.
627 * @param root the {@code rootPane} object for this frame
628 *
629 * @see #getRootPane
630 *
631 * @beaninfo
632 * hidden: true
633 * description: the RootPane object for this frame.
634 */
635 protected void setRootPane(JRootPane root)
636 {
637 if(rootPane != null) {
638 remove(rootPane);
639 }
640 rootPane = root;
641 if(rootPane != null) {
642 boolean checkingEnabled = isRootPaneCheckingEnabled();
643 try {
644 setRootPaneCheckingEnabled(false);
645 add(rootPane, BorderLayout.CENTER);
646 }
647 finally {
648 setRootPaneCheckingEnabled(checkingEnabled);
649 }
650 }
651 }
652
653 /**
654 * {@inheritDoc}
655 */
656 public void setIconImage(Image image) {
657 super.setIconImage(image);
658 }
659
660 /**
661 * Returns the {@code contentPane} object for this frame.
662 * @return the {@code contentPane} property
663 *
664 * @see #setContentPane
665 * @see RootPaneContainer#getContentPane
666 */
667 public Container getContentPane() {
668 return getRootPane().getContentPane();
669 }
670
671 /**
672 * Sets the {@code contentPane} property.
673 * This method is called by the constructor.
674 * <p>
675 * Swing's painting architecture requires an opaque {@code JComponent}
676 * in the containment hierarchy. This is typically provided by the
677 * content pane. If you replace the content pane it is recommended you
678 * replace it with an opaque {@code JComponent}.
679 *
680 * @param contentPane the {@code contentPane} object for this frame
681 *
682 * @exception java.awt.IllegalComponentStateException (a runtime
683 * exception) if the content pane parameter is {@code null}
684 * @see #getContentPane
685 * @see RootPaneContainer#setContentPane
686 * @see JRootPane
687 *
688 * @beaninfo
689 * hidden: true
690 * description: The client area of the frame where child
691 * components are normally inserted.
692 */
693 public void setContentPane(Container contentPane) {
694 getRootPane().setContentPane(contentPane);
695 }
696
697 /**
698 * Returns the {@code layeredPane} object for this frame.
699 * @return the {@code layeredPane} property
700 *
701 * @see #setLayeredPane
702 * @see RootPaneContainer#getLayeredPane
703 */
704 public JLayeredPane getLayeredPane() {
705 return getRootPane().getLayeredPane();
706 }
707
708 /**
709 * Sets the {@code layeredPane} property.
710 * This method is called by the constructor.
711 * @param layeredPane the {@code layeredPane} object for this frame
712 *
713 * @exception java.awt.IllegalComponentStateException (a runtime
714 * exception) if the layered pane parameter is {@code null}
715 * @see #getLayeredPane
716 * @see RootPaneContainer#setLayeredPane
717 *
718 * @beaninfo
719 * hidden: true
720 * description: The pane that holds the various frame layers.
721 */
722 public void setLayeredPane(JLayeredPane layeredPane) {
723 getRootPane().setLayeredPane(layeredPane);
724 }
725
726 /**
727 * Returns the {@code glassPane} object for this frame.
728 * @return the {@code glassPane} property
729 *
730 * @see #setGlassPane
731 * @see RootPaneContainer#getGlassPane
732 */
733 public Component getGlassPane() {
734 return getRootPane().getGlassPane();
735 }
736
737 /**
738 * Sets the {@code glassPane} property.
739 * This method is called by the constructor.
740 * @param glassPane the {@code glassPane} object for this frame
741 *
742 * @see #getGlassPane
743 * @see RootPaneContainer#setGlassPane
744 *
745 * @beaninfo
746 * hidden: true
747 * description: A transparent pane used for menu rendering.
748 */
749 public void setGlassPane(Component glassPane) {
750 getRootPane().setGlassPane(glassPane);
751 }
752
753 /**
754 * {@inheritDoc}
755 *
756 * @since 1.6
757 */
758 public Graphics getGraphics() {
759 JComponent.getGraphicsInvoked(this);
760 return super.getGraphics();
761 }
762
763 /**
764 * Repaints the specified rectangle of this component within
765 * {@code time} milliseconds. Refer to {@code RepaintManager}
766 * for details on how the repaint is handled.
767 *
768 * @param time maximum time in milliseconds before update
769 * @param x the <i>x</i> coordinate
770 * @param y the <i>y</i> coordinate
771 * @param width the width
772 * @param height the height
773 * @see RepaintManager
774 * @since 1.6
775 */
776 public void repaint(long time, int x, int y, int width, int height) {
777 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
778 RepaintManager.currentManager(this).addDirtyRegion(
779 this, x, y, width, height);
780 }
781 else {
782 super.repaint(time, x, y, width, height);
783 }
784 }
785
786 /**
787 * Provides a hint as to whether or not newly created {@code JFrame}s
788 * should have their Window decorations (such as borders, widgets to
789 * close the window, title...) provided by the current look
790 * and feel. If {@code defaultLookAndFeelDecorated} is true,
791 * the current {@code LookAndFeel} supports providing window
792 * decorations, and the current window manager supports undecorated
793 * windows, then newly created {@code JFrame}s will have their
794 * Window decorations provided by the current {@code LookAndFeel}.
795 * Otherwise, newly created {@code JFrame}s will have their
796 * Window decorations provided by the current window manager.
797 * <p>
798 * You can get the same effect on a single JFrame by doing the following:
799 * <pre>
800 * JFrame frame = new JFrame();
801 * frame.setUndecorated(true);
802 * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
803 * </pre>
804 *
805 * @param defaultLookAndFeelDecorated A hint as to whether or not current
806 * look and feel should provide window decorations
807 * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
808 * @since 1.4
809 */
810 public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
811 if (defaultLookAndFeelDecorated) {
812 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
813 } else {
814 SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
815 }
816 }
817
818
819 /**
820 * Returns true if newly created {@code JFrame}s should have their
821 * Window decorations provided by the current look and feel. This is only
822 * a hint, as certain look and feels may not support this feature.
823 *
824 * @return true if look and feel should provide Window decorations.
825 * @since 1.4
826 */
827 public static boolean isDefaultLookAndFeelDecorated() {
828 Boolean defaultLookAndFeelDecorated =
829 (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
830 if (defaultLookAndFeelDecorated == null) {
831 defaultLookAndFeelDecorated = Boolean.FALSE;
832 }
833 return defaultLookAndFeelDecorated.booleanValue();
834 }
835
836 /**
837 * Returns a string representation of this {@code JFrame}.
838 * This method
839 * is intended to be used only for debugging purposes, and the
840 * content and format of the returned string may vary between
841 * implementations. The returned string may be empty but may not
842 * be {@code null}.
843 *
844 * @return a string representation of this {@code JFrame}
845 */
846 protected String paramString() {
847 String defaultCloseOperationString;
848 if (defaultCloseOperation == HIDE_ON_CLOSE) {
849 defaultCloseOperationString = "HIDE_ON_CLOSE";
850 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
851 defaultCloseOperationString = "DISPOSE_ON_CLOSE";
852 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
853 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
854 } else if (defaultCloseOperation == EXIT_ON_CLOSE) {
855 defaultCloseOperationString = "EXIT_ON_CLOSE";
856 } else defaultCloseOperationString = "";
857 String rootPaneString = (rootPane != null ?
858 rootPane.toString() : "");
859 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
860 "true" : "false");
861
862 return super.paramString() +
863 ",defaultCloseOperation=" + defaultCloseOperationString +
864 ",rootPane=" + rootPaneString +
877 protected AccessibleContext accessibleContext = null;
878
879 /**
880 * Gets the AccessibleContext associated with this JFrame.
881 * For JFrames, the AccessibleContext takes the form of an
882 * AccessibleJFrame.
883 * A new AccessibleJFrame instance is created if necessary.
884 *
885 * @return an AccessibleJFrame that serves as the
886 * AccessibleContext of this JFrame
887 */
888 public AccessibleContext getAccessibleContext() {
889 if (accessibleContext == null) {
890 accessibleContext = new AccessibleJFrame();
891 }
892 return accessibleContext;
893 }
894
895 /**
896 * This class implements accessibility support for the
897 * {@code JFrame} class. It provides an implementation of the
898 * Java Accessibility API appropriate to frame user-interface
899 * elements.
900 */
901 protected class AccessibleJFrame extends AccessibleAWTFrame {
902
903 // AccessibleContext methods
904 /**
905 * Get the accessible name of this object.
906 *
907 * @return the localized name of the object -- can be null if this
908 * object does not have a name
909 */
910 public String getAccessibleName() {
911 if (accessibleName != null) {
912 return accessibleName;
913 } else {
914 if (getTitle() == null) {
915 return super.getAccessibleName();
916 } else {
917 return getTitle();
|