001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing;
026
027 import java.awt.*;
028 import java.awt.event.*;
029 import java.beans.PropertyChangeListener;
030 import java.util.Locale;
031 import java.util.Vector;
032 import java.io.Serializable;
033
034 import javax.accessibility.*;
035
036 /**
037 * An extended version of <code>java.awt.Frame</code> that adds support for
038 * the JFC/Swing component architecture.
039 * You can find task-oriented documentation about using <code>JFrame</code>
040 * in <em>The Java Tutorial</em>, in the section
041 * <a
042 href="http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html">How to Make Frames</a>.
043 *
044 * <p>
045 * The <code>JFrame</code> class is slightly incompatible with <code>Frame</code>.
046 * Like all other JFC/Swing top-level containers,
047 * a <code>JFrame</code> contains a <code>JRootPane</code> as its only child.
048 * The <b>content pane</b> provided by the root pane should,
049 * as a rule, contain
050 * all the non-menu components displayed by the <code>JFrame</code>.
051 * This is different from the AWT <code>Frame</code> case.
052 * As a conveniance <code>add</code> and its variants, <code>remove</code> and
053 * <code>setLayout</code> have been overridden to forward to the
054 * <code>contentPane</code> as necessary. This means you can write:
055 * <pre>
056 * frame.add(child);
057 * </pre>
058 * And the child will be added to the contentPane.
059 * The content pane will
060 * always be non-null. Attempting to set it to null will cause the JFrame
061 * to throw an exception. The default content pane will have a BorderLayout
062 * manager set on it.
063 * Refer to {@link javax.swing.RootPaneContainer}
064 * for details on adding, removing and setting the <code>LayoutManager</code>
065 * of a <code>JFrame</code>.
066 * <p>
067 * Unlike a <code>Frame</code>, a <code>JFrame</code> has some notion of how to
068 * respond when the user attempts to close the window. The default behavior
069 * is to simply hide the JFrame when the user closes the window. To change the
070 * default behavior, you invoke the method
071 * {@link #setDefaultCloseOperation}.
072 * To make the <code>JFrame</code> behave the same as a <code>Frame</code>
073 * instance, use
074 * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>.
075 * <p>
076 * For more information on content panes
077 * and other features that root panes provide,
078 * see <a
079 href="http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>.
080 * <p>
081 * In a multi-screen environment, you can create a <code>JFrame</code>
082 * on a different screen device. See {@link java.awt.Frame} for more
083 * information.
084 * <p>
085 * <strong>Warning:</strong> Swing is not thread safe. For more
086 * information see <a
087 * href="package-summary.html#threading">Swing's Threading
088 * Policy</a>.
089 * <p>
090 * <strong>Warning:</strong>
091 * Serialized objects of this class will not be compatible with
092 * future Swing releases. The current serialization support is
093 * appropriate for short term storage or RMI between applications running
094 * the same version of Swing. As of 1.4, support for long term storage
095 * of all JavaBeans<sup><font size="-2">TM</font></sup>
096 * has been added to the <code>java.beans</code> package.
097 * Please see {@link java.beans.XMLEncoder}.
098 *
099 * @see JRootPane
100 * @see #setDefaultCloseOperation
101 * @see java.awt.event.WindowListener#windowClosing
102 * @see javax.swing.RootPaneContainer
103 *
104 * @beaninfo
105 * attribute: isContainer true
106 * attribute: containerDelegate getContentPane
107 * description: A toplevel window which can be minimized to an icon.
108 *
109 * @version 1.119 05/05/07
110 * @author Jeff Dinkins
111 * @author Georges Saab
112 * @author David Kloba
113 */
114 public class JFrame extends Frame implements WindowConstants,
115 Accessible, RootPaneContainer,
116 TransferHandler.HasGetTransferHandler {
117 /**
118 * The exit application default window close operation. If a window
119 * has this set as the close operation and is closed in an applet,
120 * a <code>SecurityException</code> may be thrown.
121 * It is recommended you only use this in an application.
122 * <p>
123 * @since 1.3
124 */
125 public static final int EXIT_ON_CLOSE = 3;
126
127 /**
128 * Key into the AppContext, used to check if should provide decorations
129 * by default.
130 */
131 private static final Object defaultLookAndFeelDecoratedKey = new StringBuffer(
132 "JFrame.defaultLookAndFeelDecorated");
133
134 private int defaultCloseOperation = HIDE_ON_CLOSE;
135
136 /**
137 * The <code>TransferHandler</code> for this frame.
138 */
139 private TransferHandler transferHandler;
140
141 /**
142 * The <code>JRootPane</code> instance that manages the
143 * <code>contentPane</code>
144 * and optional <code>menuBar</code> for this frame, as well as the
145 * <code>glassPane</code>.
146 *
147 * @see JRootPane
148 * @see RootPaneContainer
149 */
150 protected JRootPane rootPane;
151
152 /**
153 * If true then calls to <code>add</code> and <code>setLayout</code>
154 * will be forwarded to the <code>contentPane</code>. This is initially
155 * false, but is set to true when the <code>JFrame</code> is constructed.
156 *
157 * @see #isRootPaneCheckingEnabled
158 * @see #setRootPaneCheckingEnabled
159 * @see javax.swing.RootPaneContainer
160 */
161 protected boolean rootPaneCheckingEnabled = false;
162
163 /**
164 * Constructs a new frame that is initially invisible.
165 * <p>
166 * This constructor sets the component's locale property to the value
167 * returned by <code>JComponent.getDefaultLocale</code>.
168 *
169 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
170 * returns true.
171 * @see java.awt.GraphicsEnvironment#isHeadless
172 * @see Component#setSize
173 * @see Component#setVisible
174 * @see JComponent#getDefaultLocale
175 */
176 public JFrame() throws HeadlessException {
177 super ();
178 frameInit();
179 }
180
181 /**
182 * Creates a <code>Frame</code> in the specified
183 * <code>GraphicsConfiguration</code> of
184 * a screen device and a blank title.
185 * <p>
186 * This constructor sets the component's locale property to the value
187 * returned by <code>JComponent.getDefaultLocale</code>.
188 *
189 * @param gc the <code>GraphicsConfiguration</code> that is used
190 * to construct the new <code>Frame</code>;
191 * if <code>gc</code> is <code>null</code>, the system
192 * default <code>GraphicsConfiguration</code> is assumed
193 * @exception IllegalArgumentException if <code>gc</code> is not from
194 * a screen device. This exception is always thrown when
195 * GraphicsEnvironment.isHeadless() returns true.
196 * @see java.awt.GraphicsEnvironment#isHeadless
197 * @see JComponent#getDefaultLocale
198 * @since 1.3
199 */
200 public JFrame(GraphicsConfiguration gc) {
201 super (gc);
202 frameInit();
203 }
204
205 /**
206 * Creates a new, initially invisible <code>Frame</code> with the
207 * specified title.
208 * <p>
209 * This constructor sets the component's locale property to the value
210 * returned by <code>JComponent.getDefaultLocale</code>.
211 *
212 * @param title the title for the frame
213 * @exception HeadlessException if GraphicsEnvironment.isHeadless()
214 * returns true.
215 * @see java.awt.GraphicsEnvironment#isHeadless
216 * @see Component#setSize
217 * @see Component#setVisible
218 * @see JComponent#getDefaultLocale
219 */
220 public JFrame(String title) throws HeadlessException {
221 super (title);
222 frameInit();
223 }
224
225 /**
226 * Creates a <code>JFrame</code> with the specified title and the
227 * specified <code>GraphicsConfiguration</code> of a screen device.
228 * <p>
229 * This constructor sets the component's locale property to the value
230 * returned by <code>JComponent.getDefaultLocale</code>.
231 *
232 * @param title the title to be displayed in the
233 * frame's border. A <code>null</code> value is treated as
234 * an empty string, "".
235 * @param gc the <code>GraphicsConfiguration</code> that is used
236 * to construct the new <code>JFrame</code> with;
237 * if <code>gc</code> is <code>null</code>, the system
238 * default <code>GraphicsConfiguration</code> is assumed
239 * @exception IllegalArgumentException if <code>gc</code> is not from
240 * a screen device. This exception is always thrown when
241 * GraphicsEnvironment.isHeadless() returns true.
242 * @see java.awt.GraphicsEnvironment#isHeadless
243 * @see JComponent#getDefaultLocale
244 * @since 1.3
245 */
246 public JFrame(String title, GraphicsConfiguration gc) {
247 super (title, gc);
248 frameInit();
249 }
250
251 /** Called by the constructors to init the <code>JFrame</code> properly. */
252 protected void frameInit() {
253 enableEvents(AWTEvent.KEY_EVENT_MASK
254 | AWTEvent.WINDOW_EVENT_MASK);
255 setLocale(JComponent.getDefaultLocale());
256 setRootPane(createRootPane());
257 setBackground(UIManager.getColor("control"));
258 setRootPaneCheckingEnabled(true);
259 if (JFrame.isDefaultLookAndFeelDecorated()) {
260 boolean supportsWindowDecorations = UIManager
261 .getLookAndFeel().getSupportsWindowDecorations();
262 if (supportsWindowDecorations) {
263 setUndecorated(true);
264 getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
265 }
266 }
267 sun.awt.SunToolkit.checkAndSetPolicy(this , true);
268 }
269
270 /**
271 * Called by the constructor methods to create the default
272 * <code>rootPane</code>.
273 */
274 protected JRootPane createRootPane() {
275 JRootPane rp = new JRootPane();
276 // NOTE: this uses setOpaque vs LookAndFeel.installProperty as there
277 // is NO reason for the RootPane not to be opaque. For painting to
278 // work the contentPane must be opaque, therefor the RootPane can
279 // also be opaque.
280 rp.setOpaque(true);
281 return rp;
282 }
283
284 /**
285 * Processes window events occurring on this component.
286 * Hides the window or disposes of it, as specified by the setting
287 * of the <code>defaultCloseOperation</code> property.
288 *
289 * @param e the window event
290 * @see #setDefaultCloseOperation
291 * @see java.awt.Window#processWindowEvent
292 */
293 protected void processWindowEvent(WindowEvent e) {
294 super .processWindowEvent(e);
295
296 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
297 switch (defaultCloseOperation) {
298 case HIDE_ON_CLOSE:
299 setVisible(false);
300 break;
301 case DISPOSE_ON_CLOSE:
302 dispose();
303 break;
304 case DO_NOTHING_ON_CLOSE:
305 default:
306 break;
307 case EXIT_ON_CLOSE:
308 // This needs to match the checkExit call in
309 // setDefaultCloseOperation
310 System.exit(0);
311 break;
312 }
313 }
314 }
315
316 // public void setMenuBar(MenuBar menu) {
317 // throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
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 * <p>
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>JFrame</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(
387 "defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
388 }
389 if (this .defaultCloseOperation != operation) {
390 if (operation == EXIT_ON_CLOSE) {
391 SecurityManager security = System.getSecurityManager();
392 if (security != null) {
393 security.checkExit(0);
394 }
395 }
396 int oldValue = this .defaultCloseOperation;
397 this .defaultCloseOperation = operation;
398 firePropertyChange("defaultCloseOperation", oldValue,
399 operation);
400 }
401 }
402
403 /**
404 * Returns the operation that occurs when the user
405 * initiates a "close" on this frame.
406 *
407 * @return an integer indicating the window-close operation
408 * @see #setDefaultCloseOperation
409 */
410 public int getDefaultCloseOperation() {
411 return defaultCloseOperation;
412 }
413
414 /**
415 * Sets the {@code transferHandler} property, which is a mechanism to
416 * support transfer of data into this component. Use {@code null}
417 * if the component does not support data transfer operations.
418 * <p>
419 * If the system property {@code suppressSwingDropSupport} is {@code false}
420 * (the default) and the current drop target on this component is either
421 * {@code null} or not a user-set drop target, this method will change the
422 * drop target as follows: If {@code newHandler} is {@code null} it will
423 * clear the drop target. If not {@code null} it will install a new
424 * {@code DropTarget}.
425 * <p>
426 * Note: When used with {@code JFrame}, {@code TransferHandler} only
427 * provides data import capability, as the data export related methods
428 * are currently typed to {@code JComponent}.
429 * <p>
430 * Please see
431 * <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/dnd.html">
432 * How to Use Drag and Drop and Data Transfer</a>, a section in
433 * <em>The Java Tutorial</em>, for more information.
434 *
435 * @param newHandler the new {@code TransferHandler}
436 *
437 * @see TransferHandler
438 * @see #getTransferHandler
439 * @see java.awt.Component#setDropTarget
440 * @since 1.6
441 *
442 * @beaninfo
443 * bound: true
444 * hidden: true
445 * description: Mechanism for transfer of data into the component
446 */
447 public void setTransferHandler(TransferHandler newHandler) {
448 TransferHandler oldHandler = transferHandler;
449 transferHandler = newHandler;
450 SwingUtilities.installSwingDropTargetAsNecessary(this ,
451 transferHandler);
452 firePropertyChange("transferHandler", oldHandler, newHandler);
453 }
454
455 /**
456 * Gets the <code>transferHandler</code> property.
457 *
458 * @return the value of the <code>transferHandler</code> property
459 *
460 * @see TransferHandler
461 * @see #setTransferHandler
462 * @since 1.6
463 */
464 public TransferHandler getTransferHandler() {
465 return transferHandler;
466 }
467
468 /**
469 * Just calls <code>paint(g)</code>. This method was overridden to
470 * prevent an unnecessary call to clear the background.
471 *
472 * @param g the Graphics context in which to paint
473 */
474 public void update(Graphics g) {
475 paint(g);
476 }
477
478 /**
479 * Sets the menubar for this frame.
480 * @param menubar the menubar being placed in the frame
481 *
482 * @see #getJMenuBar
483 *
484 * @beaninfo
485 * hidden: true
486 * description: The menubar for accessing pulldown menus from this frame.
487 */
488 public void setJMenuBar(JMenuBar menubar) {
489 getRootPane().setMenuBar(menubar);
490 }
491
492 /**
493 * Returns the menubar set on this frame.
494 * @return the menubar for this frame
495 *
496 * @see #setJMenuBar
497 */
498 public JMenuBar getJMenuBar() {
499 return getRootPane().getMenuBar();
500 }
501
502 /**
503 * Returns whether calls to <code>add</code> and
504 * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
505 *
506 * @return true if <code>add</code> and <code>setLayout</code>
507 * are fowarded; false otherwise
508 *
509 * @see #addImpl
510 * @see #setLayout
511 * @see #setRootPaneCheckingEnabled
512 * @see javax.swing.RootPaneContainer
513 */
514 protected boolean isRootPaneCheckingEnabled() {
515 return rootPaneCheckingEnabled;
516 }
517
518 /**
519 * Sets whether calls to <code>add</code> and
520 * <code>setLayout</code> are forwarded to the <code>contentPane</code>.
521 *
522 * @param enabled true if <code>add</code> and <code>setLayout</code>
523 * are forwarded, false if they should operate directly on the
524 * <code>JFrame</code>.
525 *
526 * @see #addImpl
527 * @see #setLayout
528 * @see #isRootPaneCheckingEnabled
529 * @see javax.swing.RootPaneContainer
530 * @beaninfo
531 * hidden: true
532 * description: Whether the add and setLayout methods are forwarded
533 */
534 protected void setRootPaneCheckingEnabled(boolean enabled) {
535 rootPaneCheckingEnabled = enabled;
536 }
537
538 /**
539 * Adds the specified child <code>Component</code>.
540 * This method is overridden to conditionally forward calls to the
541 * <code>contentPane</code>.
542 * By default, children are added to the <code>contentPane</code> instead
543 * of the frame, refer to {@link javax.swing.RootPaneContainer} for
544 * details.
545 *
546 * @param comp the component to be enhanced
547 * @param constraints the constraints to be respected
548 * @param index the index
549 * @exception IllegalArgumentException if <code>index</code> is invalid
550 * @exception IllegalArgumentException if adding the container's parent
551 * to itself
552 * @exception IllegalArgumentException if adding a window to a container
553 *
554 * @see #setRootPaneCheckingEnabled
555 * @see javax.swing.RootPaneContainer
556 */
557 protected void addImpl(Component comp, Object constraints, int index) {
558 if (isRootPaneCheckingEnabled()) {
559 getContentPane().add(comp, constraints, index);
560 } else {
561 super .addImpl(comp, constraints, index);
562 }
563 }
564
565 /**
566 * Removes the specified component from the container. If
567 * <code>comp</code> is not the <code>rootPane</code>, this will forward
568 * the call to the <code>contentPane</code>. This will do nothing if
569 * <code>comp</code> is not a child of the <code>JFrame</code> or
570 * <code>contentPane</code>.
571 *
572 * @param comp the component to be removed
573 * @throws NullPointerException if <code>comp</code> is null
574 * @see #add
575 * @see javax.swing.RootPaneContainer
576 */
577 public void remove(Component comp) {
578 if (comp == rootPane) {
579 super .remove(comp);
580 } else {
581 getContentPane().remove(comp);
582 }
583 }
584
585 /**
586 * Sets the <code>LayoutManager</code>.
587 * Overridden to conditionally forward the call to the
588 * <code>contentPane</code>.
589 * Refer to {@link javax.swing.RootPaneContainer} for
590 * more information.
591 *
592 * @param manager the <code>LayoutManager</code>
593 * @see #setRootPaneCheckingEnabled
594 * @see javax.swing.RootPaneContainer
595 */
596 public void setLayout(LayoutManager manager) {
597 if (isRootPaneCheckingEnabled()) {
598 getContentPane().setLayout(manager);
599 } else {
600 super .setLayout(manager);
601 }
602 }
603
604 /**
605 * Returns the <code>rootPane</code> object for this frame.
606 * @return the <code>rootPane</code> property
607 *
608 * @see #setRootPane
609 * @see RootPaneContainer#getRootPane
610 */
611 public JRootPane getRootPane() {
612 return rootPane;
613 }
614
615 /**
616 * Sets the <code>rootPane</code> property.
617 * This method is called by the constructor.
618 * @param root the <code>rootPane</code> object for this frame
619 *
620 * @see #getRootPane
621 *
622 * @beaninfo
623 * hidden: true
624 * description: the RootPane object for this frame.
625 */
626 protected void setRootPane(JRootPane root) {
627 if (rootPane != null) {
628 remove(rootPane);
629 }
630 rootPane = root;
631 if (rootPane != null) {
632 boolean checkingEnabled = isRootPaneCheckingEnabled();
633 try {
634 setRootPaneCheckingEnabled(false);
635 add(rootPane, BorderLayout.CENTER);
636 } finally {
637 setRootPaneCheckingEnabled(checkingEnabled);
638 }
639 }
640 }
641
642 /**
643 * {@inheritDoc}
644 */
645 public void setIconImage(Image image) {
646 super .setIconImage(image);
647 }
648
649 /**
650 * Returns the <code>contentPane</code> object for this frame.
651 * @return the <code>contentPane</code> property
652 *
653 * @see #setContentPane
654 * @see RootPaneContainer#getContentPane
655 */
656 public Container getContentPane() {
657 return getRootPane().getContentPane();
658 }
659
660 /**
661 * Sets the <code>contentPane</code> property.
662 * This method is called by the constructor.
663 * <p>
664 * Swing's painting architecture requires an opaque <code>JComponent</code>
665 * in the containment hiearchy. This is typically provided by the
666 * content pane. If you replace the content pane it is recommended you
667 * replace it with an opaque <code>JComponent</code>.
668 *
669 * @param contentPane the <code>contentPane</code> object for this frame
670 *
671 * @exception java.awt.IllegalComponentStateException (a runtime
672 * exception) if the content pane parameter is <code>null</code>
673 * @see #getContentPane
674 * @see RootPaneContainer#setContentPane
675 * @see JRootPane
676 *
677 * @beaninfo
678 * hidden: true
679 * description: The client area of the frame where child
680 * components are normally inserted.
681 */
682 public void setContentPane(Container contentPane) {
683 getRootPane().setContentPane(contentPane);
684 }
685
686 /**
687 * Returns the <code>layeredPane</code> object for this frame.
688 * @return the <code>layeredPane</code> property
689 *
690 * @see #setLayeredPane
691 * @see RootPaneContainer#getLayeredPane
692 */
693 public JLayeredPane getLayeredPane() {
694 return getRootPane().getLayeredPane();
695 }
696
697 /**
698 * Sets the <code>layeredPane</code> property.
699 * This method is called by the constructor.
700 * @param layeredPane the <code>layeredPane</code> object for this frame
701 *
702 * @exception java.awt.IllegalComponentStateException (a runtime
703 * exception) if the layered pane parameter is <code>null</code>
704 * @see #getLayeredPane
705 * @see RootPaneContainer#setLayeredPane
706 *
707 * @beaninfo
708 * hidden: true
709 * description: The pane that holds the various frame layers.
710 */
711 public void setLayeredPane(JLayeredPane layeredPane) {
712 getRootPane().setLayeredPane(layeredPane);
713 }
714
715 /**
716 * Returns the <code>glassPane</code> object for this frame.
717 * @return the <code>glassPane</code> property
718 *
719 * @see #setGlassPane
720 * @see RootPaneContainer#getGlassPane
721 */
722 public Component getGlassPane() {
723 return getRootPane().getGlassPane();
724 }
725
726 /**
727 * Sets the <code>glassPane</code> property.
728 * This method is called by the constructor.
729 * @param glassPane the <code>glassPane</code> object for this frame
730 *
731 * @see #getGlassPane
732 * @see RootPaneContainer#setGlassPane
733 *
734 * @beaninfo
735 * hidden: true
736 * description: A transparent pane used for menu rendering.
737 */
738 public void setGlassPane(Component glassPane) {
739 getRootPane().setGlassPane(glassPane);
740 }
741
742 /**
743 * {@inheritDoc}
744 *
745 * @since 1.6
746 */
747 public Graphics getGraphics() {
748 JComponent.getGraphicsInvoked(this );
749 return super .getGraphics();
750 }
751
752 /**
753 * Repaints the specified rectangle of this component within
754 * <code>time</code> milliseconds. Refer to <code>RepaintManager</code>
755 * for details on how the repaint is handled.
756 *
757 * @param time maximum time in milliseconds before update
758 * @param x the <i>x</i> coordinate
759 * @param y the <i>y</i> coordinate
760 * @param width the width
761 * @param height the height
762 * @see RepaintManager
763 * @since 1.6
764 */
765 public void repaint(long time, int x, int y, int width, int height) {
766 if (RepaintManager.HANDLE_TOP_LEVEL_PAINT) {
767 RepaintManager.currentManager(this ).addDirtyRegion(this , x,
768 y, width, height);
769 } else {
770 super .repaint(time, x, y, width, height);
771 }
772 }
773
774 /**
775 * Provides a hint as to whether or not newly created <code>JFrame</code>s
776 * should have their Window decorations (such as borders, widgets to
777 * close the window, title...) provided by the current look
778 * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
779 * the current <code>LookAndFeel</code> supports providing window
780 * decorations, and the current window manager supports undecorated
781 * windows, then newly created <code>JFrame</code>s will have their
782 * Window decorations provided by the current <code>LookAndFeel</code>.
783 * Otherwise, newly created <code>JFrame</code>s will have their
784 * Window decorations provided by the current window manager.
785 * <p>
786 * You can get the same effect on a single JFrame by doing the following:
787 * <pre>
788 * JFrame frame = new JFrame();
789 * frame.setUndecorated(true);
790 * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
791 * </pre>
792 *
793 * @param defaultLookAndFeelDecorated A hint as to whether or not current
794 * look and feel should provide window decorations
795 * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
796 * @since 1.4
797 */
798 public static void setDefaultLookAndFeelDecorated(
799 boolean defaultLookAndFeelDecorated) {
800 if (defaultLookAndFeelDecorated) {
801 SwingUtilities.appContextPut(
802 defaultLookAndFeelDecoratedKey, Boolean.TRUE);
803 } else {
804 SwingUtilities.appContextPut(
805 defaultLookAndFeelDecoratedKey, Boolean.FALSE);
806 }
807 }
808
809 /**
810 * Returns true if newly created <code>JFrame</code>s should have their
811 * Window decorations provided by the current look and feel. This is only
812 * a hint, as certain look and feels may not support this feature.
813 *
814 * @return true if look and feel should provide Window decorations.
815 * @since 1.4
816 */
817 public static boolean isDefaultLookAndFeelDecorated() {
818 Boolean defaultLookAndFeelDecorated = (Boolean) SwingUtilities
819 .appContextGet(defaultLookAndFeelDecoratedKey);
820 if (defaultLookAndFeelDecorated == null) {
821 defaultLookAndFeelDecorated = Boolean.FALSE;
822 }
823 return defaultLookAndFeelDecorated.booleanValue();
824 }
825
826 /**
827 * Returns a string representation of this <code>JFrame</code>.
828 * This method
829 * is intended to be used only for debugging purposes, and the
830 * content and format of the returned string may vary between
831 * implementations. The returned string may be empty but may not
832 * be <code>null</code>.
833 *
834 * @return a string representation of this <code>JFrame</code>
835 */
836 protected String paramString() {
837 String defaultCloseOperationString;
838 if (defaultCloseOperation == HIDE_ON_CLOSE) {
839 defaultCloseOperationString = "HIDE_ON_CLOSE";
840 } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
841 defaultCloseOperationString = "DISPOSE_ON_CLOSE";
842 } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
843 defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
844 } else if (defaultCloseOperation == 3) {
845 defaultCloseOperationString = "EXIT_ON_CLOSE";
846 } else
847 defaultCloseOperationString = "";
848 String rootPaneString = (rootPane != null ? rootPane.toString()
849 : "");
850 String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ? "true"
851 : "false");
852
853 return super .paramString() + ",defaultCloseOperation="
854 + defaultCloseOperationString + ",rootPane="
855 + rootPaneString + ",rootPaneCheckingEnabled="
856 + rootPaneCheckingEnabledString;
857 }
858
859 /////////////////
860 // Accessibility support
861 ////////////////
862
863 /** The accessible context property. */
864 protected AccessibleContext accessibleContext = null;
865
866 /**
867 * Gets the AccessibleContext associated with this JFrame.
868 * For JFrames, the AccessibleContext takes the form of an
869 * AccessibleJFrame.
870 * A new AccessibleJFrame instance is created if necessary.
871 *
872 * @return an AccessibleJFrame that serves as the
873 * AccessibleContext of this JFrame
874 */
875 public AccessibleContext getAccessibleContext() {
876 if (accessibleContext == null) {
877 accessibleContext = new AccessibleJFrame();
878 }
879 return accessibleContext;
880 }
881
882 /**
883 * This class implements accessibility support for the
884 * <code>JFrame</code> class. It provides an implementation of the
885 * Java Accessibility API appropriate to frame user-interface
886 * elements.
887 */
888 protected class AccessibleJFrame extends AccessibleAWTFrame {
889
890 // AccessibleContext methods
891 /**
892 * Get the accessible name of this object.
893 *
894 * @return the localized name of the object -- can be null if this
895 * object does not have a name
896 */
897 public String getAccessibleName() {
898 if (accessibleName != null) {
899 return accessibleName;
900 } else {
901 if (getTitle() == null) {
902 return super .getAccessibleName();
903 } else {
904 return getTitle();
905 }
906 }
907 }
908
909 /**
910 * Get the state of this object.
911 *
912 * @return an instance of AccessibleStateSet containing the current
913 * state set of the object
914 * @see AccessibleState
915 */
916 public AccessibleStateSet getAccessibleStateSet() {
917 AccessibleStateSet states = super .getAccessibleStateSet();
918
919 if (isResizable()) {
920 states.add(AccessibleState.RESIZABLE);
921 }
922 if (getFocusOwner() != null) {
923 states.add(AccessibleState.ACTIVE);
924 }
925 // FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
926 // if we can ever figure these out]]]
927 return states;
928 }
929 } // inner class AccessibleJFrame
930 }
|