1. /*
  2. * @(#)JFrame.java 1.95 03/01/28
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.beans.PropertyChangeListener;
  11. import java.util.Locale;
  12. import java.util.Vector;
  13. import java.io.Serializable;
  14. import javax.accessibility.*;
  15. /**
  16. * An extended version of <code>java.awt.Frame</code> that adds support for
  17. * the JFC/Swing component architecture.
  18. * You can find task-oriented documentation about using <code>JFrame</code>
  19. * in <em>The Java Tutorial</em>, in the section
  20. * <a
  21. href="http://java.sun.com/docs/books/tutorial/uiswing/components/frame.html">How to Make Frames</a>.
  22. *
  23. * <p>
  24. * The <code>JFrame</code> class is slightly incompatible with <code>Frame</code>.
  25. * Like all other JFC/Swing top-level containers,
  26. * a <code>JFrame</code> contains a <code>JRootPane</code> as its only child.
  27. * The <b>content pane</b> provided by the root pane should,
  28. * as a rule, contain
  29. * all the non-menu components displayed by the <code>JFrame</code>.
  30. * This is different from the AWT <code>Frame</code> case.
  31. * For example, to add a child to
  32. * an AWT frame you'd write:
  33. * <pre>
  34. * frame.add(child);
  35. * </pre>
  36. * However using <code>JFrame</code> you need to add the child
  37. * to the <code>JFrame's</code> content pane
  38. * instead:
  39. * <pre>
  40. * frame.getContentPane().add(child);
  41. * </pre>
  42. * The same is true for setting layout managers, removing components,
  43. * listing children, and so on. All these methods should normally be sent to
  44. * the content pane instead of the JFrame itself. The content pane will
  45. * always be non-null. Attempting to set it to null will cause the JFrame
  46. * to throw an exception. The default content pane will have a BorderLayout
  47. * manager set on it.
  48. * <p>
  49. * Unlike a <code>Frame</code>, a <code>JFrame</code> has some notion of how to
  50. * respond when the user attempts to close the window. The default behavior
  51. * is to simply hide the JFrame when the user closes the window. To change the
  52. * default behavior, you invoke the method
  53. * {@link #setDefaultCloseOperation}.
  54. * To make the <code>JFrame</code> behave the same as a <code>Frame</code>
  55. * instance, use
  56. * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>.
  57. * <p>
  58. * For more information on content panes
  59. * and other features that root panes provide,
  60. * see <a
  61. href="http://java.sun.com/docs/books/tutorial/uiswing/components/toplevel.html">Using Top-Level Containers</a> in <em>The Java Tutorial</em>.
  62. * <p>
  63. * In a multi-screen environment, you can create a <code>JFrame</code>
  64. * on a different screen device. See {@link java.awt.Frame} for more
  65. * information.
  66. * <p>
  67. * For the keyboard keys used by this component in the standard Look and
  68. * Feel (L&F) renditions, see the
  69. * <a href="doc-files/Key-Index.html#JFrame"><code>JFrame</code> key assignments</a>.
  70. * <p>
  71. * <strong>Warning:</strong>
  72. * Serialized objects of this class will not be compatible with
  73. * future Swing releases. The current serialization support is
  74. * appropriate for short term storage or RMI between applications running
  75. * the same version of Swing. As of 1.4, support for long term storage
  76. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  77. * has been added to the <code>java.beans</code> package.
  78. * Please see {@link java.beans.XMLEncoder}.
  79. *
  80. * @see JRootPane
  81. * @see #setDefaultCloseOperation
  82. * @see java.awt.event.WindowListener#windowClosing
  83. *
  84. * @beaninfo
  85. * attribute: isContainer true
  86. * attribute: containerDelegate getContentPane
  87. * description: A toplevel window which can be minimized to an icon.
  88. *
  89. * @version 1.95 01/28/03
  90. * @author Jeff Dinkins
  91. * @author Georges Saab
  92. * @author David Kloba
  93. */
  94. public class JFrame extends Frame implements WindowConstants, Accessible, RootPaneContainer
  95. {
  96. /**
  97. * The exit application default window close operation. If a window
  98. * has this set as the close operation and is closed in an applet,
  99. * a <code>SecurityException</code> may be thrown.
  100. * It is recommended you only use this in an application.
  101. * <p>
  102. * @since 1.3
  103. */
  104. public static final int EXIT_ON_CLOSE = 3;
  105. /**
  106. * Key into the AppContext, used to check if should provide decorations
  107. * by default.
  108. */
  109. private static final Object defaultLookAndFeelDecoratedKey =
  110. new StringBuffer("JFrame.defaultLookAndFeelDecorated");
  111. private int defaultCloseOperation = HIDE_ON_CLOSE;
  112. /**
  113. * The <code>JRootPane</code> instance that manages the
  114. * <code>contentPane</code>
  115. * and optional <code>menuBar</code> for this frame, as well as the
  116. * <code>glassPane</code>.
  117. *
  118. * @see JRootPane
  119. * @see RootPaneContainer
  120. */
  121. protected JRootPane rootPane;
  122. /**
  123. * If true then calls to <code>add</code> and <code>setLayout</code>
  124. * will cause an exception to be thrown. The default is false.
  125. *
  126. * @see #isRootPaneCheckingEnabled
  127. * @see #setRootPaneCheckingEnabled
  128. */
  129. protected boolean rootPaneCheckingEnabled = false;
  130. /**
  131. * Constructs a new frame that is initially invisible.
  132. * <p>
  133. * This constructor sets the component's locale property to the value
  134. * returned by <code>JComponent.getDefaultLocale</code>.
  135. *
  136. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  137. * returns true.
  138. * @see java.awt.GraphicsEnvironment#isHeadless
  139. * @see Component#setSize
  140. * @see Component#setVisible
  141. * @see JComponent#getDefaultLocale
  142. */
  143. public JFrame() throws HeadlessException {
  144. super();
  145. frameInit();
  146. }
  147. /**
  148. * Creates a <code>Frame</code> in the specified
  149. * <code>GraphicsConfiguration</code> of
  150. * a screen device and a blank title.
  151. * <p>
  152. * This constructor sets the component's locale property to the value
  153. * returned by <code>JComponent.getDefaultLocale</code>.
  154. *
  155. * @param gc the <code>GraphicsConfiguration</code> that is used
  156. * to construct the new <code>Frame</code>
  157. * if <code>gc</code> is <code>null</code>, the system
  158. * default <code>GraphicsConfiguration</code> is assumed
  159. * @exception IllegalArgumentException if <code>gc</code> is not from
  160. * a screen device. This exception is always thrown when
  161. * GraphicsEnvironment.isHeadless() returns true.
  162. * @see java.awt.GraphicsEnvironment#isHeadless
  163. * @see JComponent#getDefaultLocale
  164. * @since 1.3
  165. */
  166. public JFrame(GraphicsConfiguration gc) {
  167. super(gc);
  168. frameInit();
  169. }
  170. /**
  171. * Creates a new, initially invisible <code>Frame</code> with the
  172. * specified title.
  173. * <p>
  174. * This constructor sets the component's locale property to the value
  175. * returned by <code>JComponent.getDefaultLocale</code>.
  176. *
  177. * @param title the title for the frame
  178. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  179. * returns true.
  180. * @see java.awt.GraphicsEnvironment#isHeadless
  181. * @see Component#setSize
  182. * @see Component#setVisible
  183. * @see JComponent#getDefaultLocale
  184. */
  185. public JFrame(String title) throws HeadlessException {
  186. super(title);
  187. frameInit();
  188. }
  189. /**
  190. * Creates a <code>JFrame</code> with the specified title and the
  191. * specified <code>GraphicsConfiguration</code> of a screen device.
  192. * <p>
  193. * This constructor sets the component's locale property to the value
  194. * returned by <code>JComponent.getDefaultLocale</code>.
  195. *
  196. * @param title the title to be displayed in the
  197. * frame's border. A <code>null</code> value is treated as
  198. * an empty string, "".
  199. * @param gc the <code>GraphicsConfiguration</code> that is used
  200. * to construct the new <code>JFrame</code> with;
  201. * if <code>gc</code> is <code>null</code>, the system
  202. * default <code>GraphicsConfiguration</code> is assumed
  203. * @exception IllegalArgumentException if <code>gc</code> is not from
  204. * a screen device. This exception is always thrown when
  205. * GraphicsEnvironment.isHeadless() returns true.
  206. * @see java.awt.GraphicsEnvironment#isHeadless
  207. * @see JComponent#getDefaultLocale
  208. * @since 1.3
  209. */
  210. public JFrame(String title, GraphicsConfiguration gc) {
  211. super(title, gc);
  212. frameInit();
  213. }
  214. /** Called by the constructors to init the <code>JFrame</code> properly. */
  215. protected void frameInit() {
  216. enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
  217. setLocale( JComponent.getDefaultLocale() );
  218. setRootPane(createRootPane());
  219. setBackground(UIManager.getColor("control"));
  220. setRootPaneCheckingEnabled(true);
  221. if (JFrame.isDefaultLookAndFeelDecorated()) {
  222. boolean supportsWindowDecorations =
  223. UIManager.getLookAndFeel().getSupportsWindowDecorations();
  224. if (supportsWindowDecorations) {
  225. setUndecorated(true);
  226. getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
  227. }
  228. }
  229. setFocusTraversalPolicy(KeyboardFocusManager.
  230. getCurrentKeyboardFocusManager().
  231. getDefaultFocusTraversalPolicy());
  232. }
  233. /**
  234. * Called by the constructor methods to create the default
  235. * <code>rootPane</code>.
  236. */
  237. protected JRootPane createRootPane() {
  238. return new JRootPane();
  239. }
  240. /**
  241. * Processes window events occurring on this component.
  242. * Hides the window or disposes of it, as specified by the setting
  243. * of the <code>defaultCloseOperation</code> property.
  244. *
  245. * @param e the window event
  246. * @see #setDefaultCloseOperation
  247. * @see java.awt.Window#processWindowEvent
  248. */
  249. protected void processWindowEvent(WindowEvent e) {
  250. super.processWindowEvent(e);
  251. if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  252. switch(defaultCloseOperation) {
  253. case HIDE_ON_CLOSE:
  254. setVisible(false);
  255. break;
  256. case DISPOSE_ON_CLOSE:
  257. setVisible(false);
  258. dispose();
  259. break;
  260. case DO_NOTHING_ON_CLOSE:
  261. default:
  262. break;
  263. case EXIT_ON_CLOSE:
  264. // This needs to match the checkExit call in
  265. // setDefaultCloseOperation
  266. System.exit(0);
  267. break;
  268. }
  269. }
  270. }
  271. // public void setMenuBar(MenuBar menu) {
  272. // throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
  273. // }
  274. /**
  275. * Sets the operation that will happen by default when
  276. * the user initiates a "close" on this frame.
  277. * You must specify one of the following choices:
  278. * <p>
  279. * <ul>
  280. * <li><code>DO_NOTHING_ON_CLOSE</code>
  281. * (defined in <code>WindowConstants</code>):
  282. * Don't do anything; require the
  283. * program to handle the operation in the <code>windowClosing</code>
  284. * method of a registered <code>WindowListener</code> object.
  285. *
  286. * <li><code>HIDE_ON_CLOSE</code>
  287. * (defined in <code>WindowConstants</code>):
  288. * Automatically hide the frame after
  289. * invoking any registered <code>WindowListener</code>
  290. * objects.
  291. *
  292. * <li><code>DISPOSE_ON_CLOSE</code>
  293. * (defined in <code>WindowConstants</code>):
  294. * Automatically hide and dispose the
  295. * frame after invoking any registered <code>WindowListener</code>
  296. * objects.
  297. *
  298. * <li><code>EXIT_ON_CLOSE</code>
  299. * (defined in <code>JFrame</code>):
  300. * Exit the application using the <code>System</code> <code>exit</code> method.
  301. * Use this only in applications.
  302. * </ul>
  303. * <p>
  304. * The value is set to <code>HIDE_ON_CLOSE</code> by default.
  305. * <p>
  306. * <b>Note</b>: When the last displayable window within the
  307. * Java virtual machine (VM) is disposed of, the VM may
  308. * terminate. See <a href="../../java/awt/doc-files/AWTThreadIssues.html">
  309. * AWT Threading Issues</a> for more information.
  310. *
  311. * @param operation the operation which should be performed when the
  312. * user closes the frame
  313. * @exception IllegalArgumentException if defaultCloseOperation value
  314. * isn't one of the above valid values
  315. * @see #addWindowListener
  316. * @see #getDefaultCloseOperation
  317. * @see WindowConstants
  318. * @throws SecurityException
  319. * if <code>EXIT_ON_CLOSE</code> has been specified and the
  320. * <code>SecurityManager</code> will
  321. * not allow the caller to invoke <code>System.exit</code>
  322. * @see java.lang.Runtime#exit(int)
  323. *
  324. * @beaninfo
  325. * preferred: true
  326. * bound: true
  327. * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE
  328. * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
  329. * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
  330. * EXIT_ON_CLOSE WindowConstants.EXIT_ON_CLOSE
  331. * description: The frame's default close operation.
  332. */
  333. public void setDefaultCloseOperation(int operation) {
  334. if (operation != DO_NOTHING_ON_CLOSE &&
  335. operation != HIDE_ON_CLOSE &&
  336. operation != DISPOSE_ON_CLOSE &&
  337. operation != EXIT_ON_CLOSE) {
  338. throw new IllegalArgumentException("defaultCloseOperation must be one of: DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE, DISPOSE_ON_CLOSE, or EXIT_ON_CLOSE");
  339. }
  340. if (this.defaultCloseOperation != operation) {
  341. if (operation == EXIT_ON_CLOSE) {
  342. SecurityManager security = System.getSecurityManager();
  343. if (security != null) {
  344. security.checkExit(0);
  345. }
  346. }
  347. int oldValue = this.defaultCloseOperation;
  348. this.defaultCloseOperation = operation;
  349. firePropertyChange("defaultCloseOperation", oldValue, operation);
  350. }
  351. }
  352. /**
  353. * Returns the operation that occurs when the user
  354. * initiates a "close" on this frame.
  355. *
  356. * @return an integer indicating the window-close operation
  357. * @see #setDefaultCloseOperation
  358. */
  359. public int getDefaultCloseOperation() {
  360. return defaultCloseOperation;
  361. }
  362. /**
  363. * Just calls <code>paint(g)</code>. This method was overridden to
  364. * prevent an unnecessary call to clear the background.
  365. *
  366. * @param g the Graphics context in which to paint
  367. */
  368. public void update(Graphics g) {
  369. paint(g);
  370. }
  371. /**
  372. * Sets the menubar for this frame.
  373. * @param menubar the menubar being placed in the frame
  374. *
  375. * @see #getJMenuBar
  376. *
  377. * @beaninfo
  378. * hidden: true
  379. * description: The menubar for accessing pulldown menus from this frame.
  380. */
  381. public void setJMenuBar(JMenuBar menubar) {
  382. getRootPane().setMenuBar(menubar);
  383. }
  384. /**
  385. * Returns the menubar set on this frame.
  386. * @return the menubar for this frame
  387. *
  388. * @see #setJMenuBar
  389. */
  390. public JMenuBar getJMenuBar() {
  391. return getRootPane().getMenuBar();
  392. }
  393. /**
  394. * Returns whether calls to <code>add</code> and
  395. * <code>setLayout</code> cause an exception to be thrown.
  396. *
  397. * @return true if <code>add</code> and <code>setLayout</code>
  398. * are checked; false otherwise
  399. *
  400. * @see #addImpl
  401. * @see #setLayout
  402. * @see #setRootPaneCheckingEnabled
  403. */
  404. protected boolean isRootPaneCheckingEnabled() {
  405. return rootPaneCheckingEnabled;
  406. }
  407. /**
  408. * Determines whether calls to <code>add</code> and
  409. * <code>setLayout</code> will cause an exception to be thrown.
  410. *
  411. * @param enabled true if checking is to be
  412. * enabled, which causes the exceptions to be thrown
  413. *
  414. * @see #addImpl
  415. * @see #setLayout
  416. * @see #isRootPaneCheckingEnabled
  417. * @beaninfo
  418. * hidden: true
  419. * description: Whether the add and setLayout methods throw exceptions when invoked.
  420. */
  421. protected void setRootPaneCheckingEnabled(boolean enabled) {
  422. rootPaneCheckingEnabled = enabled;
  423. }
  424. /**
  425. * Creates a runtime exception with a message like:
  426. * <pre>
  427. * "Do not use JFrame.add() use JFrame.getContentPane().add() instead"
  428. * </pre>
  429. *
  430. * @param op a <code>String</code> indicating the attempted operation;
  431. * in the example above, the operation string is "add"
  432. */
  433. private Error createRootPaneException(String op) {
  434. String type = getClass().getName();
  435. return new Error(
  436. "Do not use " + type + "." + op + "() use "
  437. + type + ".getContentPane()." + op + "() instead");
  438. }
  439. /**
  440. * By default, children may not be added directly to this component,
  441. * they must be added to its contentPane instead. For example:
  442. * <pre>
  443. * thisComponent.getContentPane().add(child)
  444. * </pre>
  445. * An attempt to add to directly to this component will cause an
  446. * runtime exception to be thrown. Subclasses can disable this
  447. * behavior.
  448. *
  449. * @param comp the component to be enhanced
  450. * @param constraints the constraints to be respected
  451. * @param index the index
  452. * @exception Error if called with <code>rootPaneChecking</code> true
  453. *
  454. * @see #setRootPaneCheckingEnabled
  455. */
  456. protected void addImpl(Component comp, Object constraints, int index)
  457. {
  458. if(isRootPaneCheckingEnabled()) {
  459. throw createRootPaneException("add");
  460. }
  461. else {
  462. super.addImpl(comp, constraints, index);
  463. }
  464. }
  465. /**
  466. * Removes the specified component from this container.
  467. * @param comp the component to be removed
  468. * @see #add
  469. */
  470. public void remove(Component comp) {
  471. if (comp == rootPane) {
  472. super.remove(comp);
  473. } else {
  474. // Client mistake, but we need to handle it to avoid a
  475. // common object leak in client applications.
  476. getContentPane().remove(comp);
  477. }
  478. }
  479. /**
  480. * By default the layout of this component may not be set,
  481. * the layout of its <code>contentPane</code> should be set instead.
  482. * For example:
  483. * <pre>
  484. * thisComponent.getContentPane().setLayout(new GridLayout(1, 2))
  485. * </pre>
  486. * An attempt to set the layout of this component will cause an
  487. * runtime exception to be thrown. Subclasses can disable this
  488. * behavior.
  489. * @param manager the <code>LayoutManager</code>
  490. * @exception Error if called with <code>rootPaneChecking</code> true
  491. *
  492. * @see #setRootPaneCheckingEnabled
  493. */
  494. public void setLayout(LayoutManager manager) {
  495. if(isRootPaneCheckingEnabled()) {
  496. throw createRootPaneException("setLayout");
  497. }
  498. else {
  499. super.setLayout(manager);
  500. }
  501. }
  502. /**
  503. * Returns the <code>rootPane</code> object for this frame.
  504. * @return the <code>rootPane</code> property
  505. *
  506. * @see #setRootPane
  507. * @see RootPaneContainer#getRootPane
  508. */
  509. public JRootPane getRootPane() {
  510. return rootPane;
  511. }
  512. /**
  513. * Sets the <code>rootPane</code> property.
  514. * This method is called by the constructor.
  515. * @param root the <code>rootPane</code> object for this frame
  516. *
  517. * @see #getRootPane
  518. *
  519. * @beaninfo
  520. * hidden: true
  521. * description: the RootPane object for this frame.
  522. */
  523. protected void setRootPane(JRootPane root)
  524. {
  525. if(rootPane != null) {
  526. remove(rootPane);
  527. }
  528. rootPane = root;
  529. if(rootPane != null) {
  530. boolean checkingEnabled = isRootPaneCheckingEnabled();
  531. try {
  532. setRootPaneCheckingEnabled(false);
  533. add(rootPane, BorderLayout.CENTER);
  534. }
  535. finally {
  536. setRootPaneCheckingEnabled(checkingEnabled);
  537. }
  538. }
  539. }
  540. /**
  541. * Returns the <code>contentPane</code> object for this frame.
  542. * @return the <code>contentPane</code> property
  543. *
  544. * @see #setContentPane
  545. * @see RootPaneContainer#getContentPane
  546. */
  547. public Container getContentPane() {
  548. return getRootPane().getContentPane();
  549. }
  550. /**
  551. * Sets the <code>contentPane</code> property.
  552. * This method is called by the constructor.
  553. * <p>
  554. * Swing's painting architecture requires an opaque <code>JComponent</code>
  555. * in the containment hiearchy. This is typically provided by the
  556. * content pane. If you replace the content pane it is recommended you
  557. * replace it with an opaque <code>JComponent</code>.
  558. *
  559. * @param contentPane the <code>contentPane</code> object for this frame
  560. *
  561. * @exception java.awt.IllegalComponentStateException (a runtime
  562. * exception) if the content pane parameter is <code>null</code>
  563. * @see #getContentPane
  564. * @see RootPaneContainer#setContentPane
  565. * @see JRootPane
  566. *
  567. * @beaninfo
  568. * hidden: true
  569. * description: The client area of the frame where child
  570. * components are normally inserted.
  571. */
  572. public void setContentPane(Container contentPane) {
  573. getRootPane().setContentPane(contentPane);
  574. }
  575. /**
  576. * Returns the <code>layeredPane</code> object for this frame.
  577. * @return the <code>layeredPane</code> property
  578. *
  579. * @see #setLayeredPane
  580. * @see RootPaneContainer#getLayeredPane
  581. */
  582. public JLayeredPane getLayeredPane() {
  583. return getRootPane().getLayeredPane();
  584. }
  585. /**
  586. * Sets the <code>layeredPane</code> property.
  587. * This method is called by the constructor.
  588. * @param layeredPane the <code>layeredPane</code> object for this frame
  589. *
  590. * @exception java.awt.IllegalComponentStateException (a runtime
  591. * exception) if the layered pane parameter is <code>null</code>
  592. * @see #getLayeredPane
  593. * @see RootPaneContainer#setLayeredPane
  594. *
  595. * @beaninfo
  596. * hidden: true
  597. * description: The pane that holds the various frame layers.
  598. */
  599. public void setLayeredPane(JLayeredPane layeredPane) {
  600. getRootPane().setLayeredPane(layeredPane);
  601. }
  602. /**
  603. * Returns the <code>glassPane</code> object for this frame.
  604. * @return the <code>glassPane</code> property
  605. *
  606. * @see #setGlassPane
  607. * @see RootPaneContainer#getGlassPane
  608. */
  609. public Component getGlassPane() {
  610. return getRootPane().getGlassPane();
  611. }
  612. /**
  613. * Sets the <code>glassPane</code> property.
  614. * This method is called by the constructor.
  615. * @param glassPane the <code>glassPane</code> object for this frame
  616. *
  617. * @see #getGlassPane
  618. * @see RootPaneContainer#setGlassPane
  619. *
  620. * @beaninfo
  621. * hidden: true
  622. * description: A transparent pane used for menu rendering.
  623. */
  624. public void setGlassPane(Component glassPane) {
  625. getRootPane().setGlassPane(glassPane);
  626. }
  627. /**
  628. * Provides a hint as to whether or not newly created <code>JFrame</code>s
  629. * should have their Window decorations (such as borders, widgets to
  630. * close the window, title...) provided by the current look
  631. * and feel. If <code>defaultLookAndFeelDecorated</code> is true,
  632. * the current <code>LookAndFeel</code> supports providing window
  633. * decorations, and the current window manager supports undecorated
  634. * windows, then newly created <code>JFrame</code>s will have their
  635. * Window decorations provided by the current <code>LookAndFeel</code>.
  636. * Otherwise, newly created <code>JFrame</code>s will have their
  637. * Window decorations provided by the current window manager.
  638. * <p>
  639. * You can get the same effect on a single JFrame by doing the following:
  640. * <pre>
  641. * JFrame frame = new JFrame();
  642. * frame.setUndecorated(true);
  643. * frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
  644. * </pre>
  645. *
  646. * @param defaultLookAndFeelDecorated A hint as to whether or not current
  647. * look and feel should provide window decorations
  648. * @see javax.swing.LookAndFeel#getSupportsWindowDecorations
  649. * @since 1.4
  650. */
  651. public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) {
  652. if (defaultLookAndFeelDecorated) {
  653. SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.TRUE);
  654. } else {
  655. SwingUtilities.appContextPut(defaultLookAndFeelDecoratedKey, Boolean.FALSE);
  656. }
  657. }
  658. /**
  659. * Returns true if newly created <code>JFrame</code>s should have their
  660. * Window decorations provided by the current look and feel. This is only
  661. * a hint, as certain look and feels may not support this feature.
  662. *
  663. * @return true if look and feel should provide Window decorations.
  664. * @since 1.4
  665. */
  666. public static boolean isDefaultLookAndFeelDecorated() {
  667. Boolean defaultLookAndFeelDecorated =
  668. (Boolean) SwingUtilities.appContextGet(defaultLookAndFeelDecoratedKey);
  669. if (defaultLookAndFeelDecorated == null) {
  670. defaultLookAndFeelDecorated = Boolean.FALSE;
  671. }
  672. return defaultLookAndFeelDecorated.booleanValue();
  673. }
  674. /**
  675. * Returns a string representation of this <code>JFrame</code>.
  676. * This method
  677. * is intended to be used only for debugging purposes, and the
  678. * content and format of the returned string may vary between
  679. * implementations. The returned string may be empty but may not
  680. * be <code>null</code>.
  681. *
  682. * @return a string representation of this <code>JFrame</code>
  683. */
  684. protected String paramString() {
  685. String defaultCloseOperationString;
  686. if (defaultCloseOperation == HIDE_ON_CLOSE) {
  687. defaultCloseOperationString = "HIDE_ON_CLOSE";
  688. } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
  689. defaultCloseOperationString = "DISPOSE_ON_CLOSE";
  690. } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
  691. defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
  692. } else if (defaultCloseOperation == 3) {
  693. defaultCloseOperationString = "EXIT_ON_CLOSE";
  694. } else defaultCloseOperationString = "";
  695. String rootPaneString = (rootPane != null ?
  696. rootPane.toString() : "");
  697. String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
  698. "true" : "false");
  699. return super.paramString() +
  700. ",defaultCloseOperation=" + defaultCloseOperationString +
  701. ",rootPane=" + rootPaneString +
  702. ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
  703. }
  704. /////////////////
  705. // Accessibility support
  706. ////////////////
  707. /** The accessible context property. */
  708. protected AccessibleContext accessibleContext = null;
  709. /**
  710. * Gets the AccessibleContext associated with this JFrame.
  711. * For JFrames, the AccessibleContext takes the form of an
  712. * AccessibleJFrame.
  713. * A new AccessibleJFrame instance is created if necessary.
  714. *
  715. * @return an AccessibleJFrame that serves as the
  716. * AccessibleContext of this JFrame
  717. */
  718. public AccessibleContext getAccessibleContext() {
  719. if (accessibleContext == null) {
  720. accessibleContext = new AccessibleJFrame();
  721. }
  722. return accessibleContext;
  723. }
  724. /**
  725. * This class implements accessibility support for the
  726. * <code>JFrame</code> class. It provides an implementation of the
  727. * Java Accessibility API appropriate to frame user-interface
  728. * elements.
  729. */
  730. protected class AccessibleJFrame extends AccessibleAWTFrame {
  731. // AccessibleContext methods
  732. /**
  733. * Get the accessible name of this object.
  734. *
  735. * @return the localized name of the object -- can be null if this
  736. * object does not have a name
  737. */
  738. public String getAccessibleName() {
  739. if (accessibleName != null) {
  740. return accessibleName;
  741. } else {
  742. if (getTitle() == null) {
  743. return super.getAccessibleName();
  744. } else {
  745. return getTitle();
  746. }
  747. }
  748. }
  749. /**
  750. * Get the state of this object.
  751. *
  752. * @return an instance of AccessibleStateSet containing the current
  753. * state set of the object
  754. * @see AccessibleState
  755. */
  756. public AccessibleStateSet getAccessibleStateSet() {
  757. AccessibleStateSet states = super.getAccessibleStateSet();
  758. if (isResizable()) {
  759. states.add(AccessibleState.RESIZABLE);
  760. }
  761. if (getFocusOwner() != null) {
  762. states.add(AccessibleState.ACTIVE);
  763. }
  764. // FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
  765. // if we can ever figure these out]]]
  766. return states;
  767. }
  768. } // inner class AccessibleJFrame
  769. }