1. /*
  2. * @(#)JFrame.java 1.67 02/05/01
  3. *
  4. * Copyright 2002 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 java.awt.Frame that adds support for
  17. * interposing input and painting behavior in front of the frame's
  18. * children (see glassPane), support for special children that
  19. * are managed by a LayeredPane (see rootPane) and for Swing MenuBars.
  20. * <p>
  21. * The JFrame class is slightly incompatible with java.awt.Frame.
  22. * JFrame contains a JRootPane as it's only child.
  23. * The <b>contentPane</b> should be the parent of any children of the JFrame.
  24. * This is different than java.awt.Frame, e.g. to add a child to
  25. * an AWT Frame you'd write:
  26. * <pre>
  27. * frame.add(child);
  28. * </pre>
  29. * However using JFrame you need to add the child to the JFrames contentPane
  30. * instead:
  31. * <pre>
  32. * frame.getContentPane().add(child);
  33. * </pre>
  34. * The same is true for setting LayoutManagers, removing components,
  35. * listing children, etc. All these methods should normally be sent to
  36. * the contentPane() instead of the JFrame itself. The contentPane() will
  37. * always be non-null. Attempting to set it to null will cause the JFrame
  38. * to throw an exception. The default contentPane() will have a BorderLayout
  39. * manager set on it.
  40. * <p>
  41. * Please see the JRootPane documentation for a complete description of
  42. * the contentPane, glassPane, and layeredPane properties.
  43. * <p>
  44. * Unlike its parent class, java.awt.Frame, you can tell a JFrame how to
  45. * respond when the user attempts to close the window. The default behavior
  46. * is to simply hide the JFrame when the user closes the window. To change the
  47. * default behavior, you invoke the method <code>setDefaultCloseOperation</code>.
  48. * To make the JFrame remain open unless you handle the window-closing event and
  49. * explicitly invoke <code>dispose()</code> (or exit the app, which is also pretty
  50. * effective), use
  51. * <code>setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)</code>.
  52. * That makes the JFrame behave the same as java.awt.Frame. A third option
  53. * lets you completely dispose of the window when it closes, instead of merely
  54. * hiding it.
  55. * <p>
  56. * For the keyboard keys used by this component in the standard Look and
  57. * Feel (L&F) renditions, see the
  58. * <a href="doc-files/Key-Index.html#JFrame">JFrame</a> key assignments.
  59. * <p>
  60. * <strong>Warning:</strong>
  61. * Serialized objects of this class will not be compatible with
  62. * future Swing releases. The current serialization support is appropriate
  63. * for short term storage or RMI between applications running the same
  64. * version of Swing. A future release of Swing will provide support for
  65. * long term persistence.
  66. *
  67. * @see JRootPane
  68. * @see #setDefaultCloseOperation
  69. * @see java.awt.event.WindowListener#windowClosing
  70. *
  71. * @beaninfo
  72. * attribute: isContainer true
  73. * attribute: containerDelegate getContentPane
  74. * description: A toplevel window which can be minimized to an icon.
  75. *
  76. * @version 1.67 05/01/02
  77. * @author Jeff Dinkins
  78. * @author Georges Saab
  79. * @author David Kloba
  80. */
  81. public class JFrame extends Frame implements WindowConstants, Accessible, RootPaneContainer
  82. {
  83. private int defaultCloseOperation = HIDE_ON_CLOSE;
  84. /**
  85. * The JRootPane instance that manages the <code>contentPane</code>
  86. * and optional <code>menuBar</code> for this frame, as well as the
  87. * <code>glassPane</code>.
  88. *
  89. * @see JRootPane
  90. * @see RootPaneContainer
  91. */
  92. protected JRootPane rootPane;
  93. /**
  94. * If true then calls to <code>add</code> and <code>setLayout</code>
  95. * cause an exception to be thrown.
  96. *
  97. * @see #isRootPaneCheckingEnabled
  98. * @see #setRootPaneCheckingEnabled
  99. */
  100. protected boolean rootPaneCheckingEnabled = false;
  101. /**
  102. * Constructs a new Frame that is initially invisible.
  103. *
  104. * @see Component#setSize
  105. * @see Component#setVisible
  106. */
  107. public JFrame() {
  108. super();
  109. frameInit();
  110. }
  111. /**
  112. * Constructs a new, initially invisible Frame with the specified
  113. * title.
  114. *
  115. * @param title the title for the frame
  116. * @see Component#setSize
  117. * @see Component#setVisible
  118. */
  119. public JFrame(String title) {
  120. super(title);
  121. frameInit();
  122. }
  123. /** Called by the constructors to init the JFrame properly. */
  124. protected void frameInit() {
  125. enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.WINDOW_EVENT_MASK);
  126. /*if[JDK1.2]
  127. // workaround for bug 4170760
  128. enableInputMethods(false);
  129. end[JDK1.2]*/
  130. setRootPane(createRootPane());
  131. setBackground(UIManager.getColor("control"));
  132. setRootPaneCheckingEnabled(true);
  133. }
  134. /** Called by the constructor methods to create the default rootPane. */
  135. protected JRootPane createRootPane() {
  136. return new JRootPane();
  137. }
  138. /**
  139. * Processes key events occurring on this component and, if appropriate,
  140. * passes them on to components in the frame which have registered
  141. * interest in them.
  142. *
  143. * @param e the key event
  144. * @see java.awt.Component#processKeyEvent
  145. */
  146. protected void processKeyEvent(KeyEvent e) {
  147. super.processKeyEvent(e);
  148. if(!e.isConsumed()) {
  149. JComponent.processKeyBindingsForAllComponents(e,this,e.getID() == KeyEvent.KEY_PRESSED);
  150. }
  151. }
  152. /**
  153. * Processes window events occurring on this component.
  154. * Hides the window or disposes of it, as specified by the setting
  155. * of the <code>defaultCloseOperation</code> property.
  156. *
  157. * @param e the window event
  158. * @see #setDefaultCloseOperation
  159. * @see java.awt.Window#processWindowEvent
  160. */
  161. protected void processWindowEvent(WindowEvent e) {
  162. super.processWindowEvent(e);
  163. if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  164. switch(defaultCloseOperation) {
  165. case HIDE_ON_CLOSE:
  166. setVisible(false);
  167. break;
  168. case DISPOSE_ON_CLOSE:
  169. setVisible(false);
  170. dispose();
  171. break;
  172. case DO_NOTHING_ON_CLOSE:
  173. default:
  174. break;
  175. case 3: // EXIT_ON_CLOSE:
  176. System.exit(0);
  177. break;
  178. }
  179. }
  180. }
  181. // public void setMenuBar(MenuBar menu) {
  182. // throw new IllegalComponentStateException("Please use setJMenuBar() with JFrame.");
  183. // }
  184. /**
  185. * Sets the operation which will happen by default when
  186. * the user initiates a "close" on this frame.
  187. * The possible choices are defined in the <code>WindowConstants</code>
  188. * interface:
  189. * <p>
  190. * <ul>
  191. * <li>DO_NOTHING_ON_CLOSE - do not do anything - require the
  192. * program to handle the operation in the windowClosing
  193. * method of a registered WindowListener object.
  194. * <li>HIDE_ON_CLOSE - automatically hide the frame after
  195. * invoking any registered WindowListener objects
  196. * <li>DISPOSE_ON_CLOSE - automatically hide and dispose the
  197. * frame after invoking any registered WindowListener objects
  198. * <li>EXIT_ON_CLOSE - Exit the application by way of System.exit.
  199. * Only use this in applications.
  200. * </ul>
  201. * <p>
  202. * The value is set to HIDE_ON_CLOSE by default.
  203. * @see #addWindowListener
  204. * @see #getDefaultCloseOperation
  205. *
  206. * @beaninfo
  207. * preferred: true
  208. * enum: DO_NOTHING_ON_CLOSE WindowConstants.DO_NOTHING_ON_CLOSE
  209. * HIDE_ON_CLOSE WindowConstants.HIDE_ON_CLOSE
  210. * DISPOSE_ON_CLOSE WindowConstants.DISPOSE_ON_CLOSE
  211. * EXIT_ON_CLOSE 3
  212. * description: The frame's default close operation.
  213. */
  214. public void setDefaultCloseOperation(int operation) {
  215. if (operation == 3) {
  216. SecurityManager security = System.getSecurityManager();
  217. if (security != null) {
  218. security.checkExit(0);
  219. }
  220. }
  221. this.defaultCloseOperation = operation;
  222. }
  223. /**
  224. * Returns the operation which occurs when the user
  225. * initiates a "close" on this frame.
  226. *
  227. * @return an int indicating the window-close operation
  228. * @see #setDefaultCloseOperation
  229. */
  230. public int getDefaultCloseOperation() {
  231. return defaultCloseOperation;
  232. }
  233. /**
  234. * Just calls <code>paint(g)</code>. This method was overridden to
  235. * prevent an unneccessary call to clear the background.
  236. *
  237. * @param g the Graphics context in which to paint
  238. */
  239. public void update(Graphics g) {
  240. paint(g);
  241. }
  242. /**
  243. * Sets the menubar for this frame.
  244. * @param menubar the menubar being placed in the frame
  245. *
  246. * @see #getJMenuBar
  247. *
  248. * @beaninfo
  249. * hidden: true
  250. * description: The menubar for accessing pulldown menus from this frame.
  251. */
  252. public void setJMenuBar(JMenuBar menubar) {
  253. getRootPane().setMenuBar(menubar);
  254. }
  255. /**
  256. * Returns the menubar set on this frame.
  257. *
  258. * @see #setJMenuBar
  259. */
  260. public JMenuBar getJMenuBar() {
  261. return getRootPane().getMenuBar();
  262. }
  263. /**
  264. * Returns whether calls to <code>add</code> and
  265. * <code>setLayout</code> cause an exception to be thrown.
  266. *
  267. * @return true if <code>add</code> and <code>setLayout</code>
  268. * are checked
  269. *
  270. * @see #addImpl
  271. * @see #setLayout
  272. * @see #setRootPaneCheckingEnabled
  273. */
  274. protected boolean isRootPaneCheckingEnabled() {
  275. return rootPaneCheckingEnabled;
  276. }
  277. /**
  278. * Determines whether calls to <code>add</code> and
  279. * <code>setLayout</code> cause an exception to be thrown.
  280. *
  281. * @param enabled a boolean value, true if checking is to be
  282. * enabled, which cause the exceptions to be thrown
  283. *
  284. * @see #addImpl
  285. * @see #setLayout
  286. * @see #isRootPaneCheckingEnabled
  287. * @beaninfo
  288. * hidden: true
  289. * description: Whether the add and setLayout methods throw exceptions when invoked.
  290. */
  291. protected void setRootPaneCheckingEnabled(boolean enabled) {
  292. rootPaneCheckingEnabled = enabled;
  293. }
  294. /**
  295. * Creates a runtime exception with a message like:
  296. * <pre>
  297. * "Do not use JFrame.add() use JFrame.getContentPane().add() instead"
  298. * </pre>
  299. *
  300. * @param op a String indicating the attempted operation. In the
  301. * example above, the operation string is "add"
  302. */
  303. private Error createRootPaneException(String op) {
  304. String type = getClass().getName();
  305. return new Error(
  306. "Do not use " + type + "." + op + "() use "
  307. + type + ".getContentPane()." + op + "() instead");
  308. }
  309. /**
  310. * By default, children may not be added directly to a this component,
  311. * they must be added to its contentPane instead. For example:
  312. * <pre>
  313. * thisComponent.getContentPane().add(child)
  314. * </pre>
  315. * An attempt to add to directly to this component will cause an
  316. * runtime exception to be thrown. Subclasses can disable this
  317. * behavior.
  318. *
  319. * @see #setRootPaneCheckingEnabled
  320. * @exception Error if called with rootPaneChecking true
  321. */
  322. protected void addImpl(Component comp, Object constraints, int index)
  323. {
  324. if(isRootPaneCheckingEnabled()) {
  325. throw createRootPaneException("add");
  326. }
  327. else {
  328. super.addImpl(comp, constraints, index);
  329. }
  330. }
  331. /**
  332. * Removes the specified component from this container.
  333. * @param comp the component to be removed
  334. * @see #add
  335. */
  336. public void remove(Component comp) {
  337. if (comp == rootPane) {
  338. super.remove(comp);
  339. } else {
  340. // Client mistake, but we need to handle it to avoid a
  341. // common object leak in client applications.
  342. getContentPane().remove(comp);
  343. }
  344. }
  345. /**
  346. * By default the layout of this component may not be set,
  347. * the layout of its contentPane should be set instead.
  348. * For example:
  349. * <pre>
  350. * thiComponent.getContentPane().setLayout(new BorderLayout())
  351. * </pre>
  352. * An attempt to set the layout of this component will cause an
  353. * runtime exception to be thrown. Subclasses can disable this
  354. * behavior.
  355. *
  356. * @see #setRootPaneCheckingEnabled
  357. * @exception Error if called with rootPaneChecking true
  358. */
  359. public void setLayout(LayoutManager manager) {
  360. if(isRootPaneCheckingEnabled()) {
  361. throw createRootPaneException("setLayout");
  362. }
  363. else {
  364. super.setLayout(manager);
  365. }
  366. }
  367. /**
  368. * Returns the rootPane object for this frame.
  369. *
  370. * @see #setRootPane
  371. * @see RootPaneContainer#getRootPane
  372. */
  373. public JRootPane getRootPane() {
  374. return rootPane;
  375. }
  376. /**
  377. * Sets the rootPane property. This method is called by the constructor.
  378. * @param root the rootPane object for this frame
  379. *
  380. * @see #getRootPane
  381. *
  382. * @beaninfo
  383. * hidden: true
  384. * description: the RootPane object for this frame.
  385. */
  386. protected void setRootPane(JRootPane root)
  387. {
  388. if(rootPane != null) {
  389. remove(rootPane);
  390. }
  391. rootPane = root;
  392. if(rootPane != null) {
  393. boolean checkingEnabled = isRootPaneCheckingEnabled();
  394. try {
  395. setRootPaneCheckingEnabled(false);
  396. add(rootPane, BorderLayout.CENTER);
  397. }
  398. finally {
  399. setRootPaneCheckingEnabled(checkingEnabled);
  400. }
  401. }
  402. }
  403. /**
  404. * Returns the contentPane object for this frame.
  405. *
  406. * @see #setContentPane
  407. * @see RootPaneContainer#getContentPane
  408. */
  409. public Container getContentPane() {
  410. return getRootPane().getContentPane();
  411. }
  412. /**
  413. * Sets the contentPane property. This method is called by the constructor.
  414. * @param contentPane the contentPane object for this frame
  415. *
  416. * @exception java.awt.IllegalComponentStateException (a runtime
  417. * exception) if the content pane parameter is null
  418. * @see #getContentPane
  419. * @see RootPaneContainer#setContentPane
  420. *
  421. * @beaninfo
  422. * hidden: true
  423. * description: The client area of the frame where child
  424. * components are normally inserted.
  425. */
  426. public void setContentPane(Container contentPane) {
  427. getRootPane().setContentPane(contentPane);
  428. }
  429. /**
  430. * Returns the layeredPane object for this frame.
  431. *
  432. * @see #setLayeredPane
  433. * @see RootPaneContainer#getLayeredPane
  434. */
  435. public JLayeredPane getLayeredPane() {
  436. return getRootPane().getLayeredPane();
  437. }
  438. /**
  439. * Sets the layeredPane property. This method is called by the constructor.
  440. * @param layeredPane the layeredPane object for this frame
  441. *
  442. * @exception java.awt.IllegalComponentStateException (a runtime
  443. * exception) if the layered pane parameter is null
  444. * @see #getLayeredPane
  445. * @see RootPaneContainer#setLayeredPane
  446. *
  447. * @beaninfo
  448. * hidden: true
  449. * description: The pane which holds the various frame layers.
  450. */
  451. public void setLayeredPane(JLayeredPane layeredPane) {
  452. getRootPane().setLayeredPane(layeredPane);
  453. }
  454. /**
  455. * Returns the glassPane object for this frame.
  456. *
  457. * @see #setGlassPane
  458. * @see RootPaneContainer#getGlassPane
  459. */
  460. public Component getGlassPane() {
  461. return getRootPane().getGlassPane();
  462. }
  463. /**
  464. * Sets the glassPane property.
  465. * This method is called by the constructor.
  466. * @param glassPane the glassPane object for this frame
  467. *
  468. * @see #getGlassPane
  469. * @see RootPaneContainer#setGlassPane
  470. *
  471. * @beaninfo
  472. * hidden: true
  473. * description: A transparent pane used for menu rendering.
  474. */
  475. public void setGlassPane(Component glassPane) {
  476. getRootPane().setGlassPane(glassPane);
  477. }
  478. /**
  479. * Returns a string representation of this JFrame. This method
  480. * is intended to be used only for debugging purposes, and the
  481. * content and format of the returned string may vary between
  482. * implementations. The returned string may be empty but may not
  483. * be <code>null</code>.
  484. *
  485. * @return a string representation of this JFrame.
  486. */
  487. protected String paramString() {
  488. String defaultCloseOperationString;
  489. if (defaultCloseOperation == HIDE_ON_CLOSE) {
  490. defaultCloseOperationString = "HIDE_ON_CLOSE";
  491. } else if (defaultCloseOperation == DISPOSE_ON_CLOSE) {
  492. defaultCloseOperationString = "DISPOSE_ON_CLOSE";
  493. } else if (defaultCloseOperation == DO_NOTHING_ON_CLOSE) {
  494. defaultCloseOperationString = "DO_NOTHING_ON_CLOSE";
  495. } else if (defaultCloseOperation == 3) {
  496. defaultCloseOperationString = "EXIT_ON_CLOSE";
  497. } else defaultCloseOperationString = "";
  498. String rootPaneString = (rootPane != null ?
  499. rootPane.toString() : "");
  500. String rootPaneCheckingEnabledString = (rootPaneCheckingEnabled ?
  501. "true" : "false");
  502. return super.paramString() +
  503. ",defaultCloseOperation=" + defaultCloseOperationString +
  504. ",rootPane=" + rootPaneString +
  505. ",rootPaneCheckingEnabled=" + rootPaneCheckingEnabledString;
  506. }
  507. /////////////////
  508. // Accessibility support
  509. ////////////////
  510. /** The accessible context property */
  511. protected AccessibleContext accessibleContext = null;
  512. /**
  513. * Get the AccessibleContext associated with this JFrame
  514. *
  515. * @return the AccessibleContext of this JFrame
  516. */
  517. public AccessibleContext getAccessibleContext() {
  518. if (accessibleContext == null) {
  519. accessibleContext = new AccessibleJFrame();
  520. }
  521. return accessibleContext;
  522. }
  523. /**
  524. * The class used to obtain the AccessibleRole for this object.
  525. */
  526. protected class AccessibleJFrame extends AccessibleContext
  527. implements Serializable, AccessibleComponent {
  528. // AccessibleContext methods
  529. /**
  530. * Get the accessible name of this object.
  531. *
  532. * @return the localized name of the object -- can be null if this
  533. * object does not have a name
  534. */
  535. public String getAccessibleName() {
  536. if (accessibleName != null) {
  537. return accessibleName;
  538. } else {
  539. if (getTitle() == null) {
  540. return super.getAccessibleName();
  541. } else {
  542. return getTitle();
  543. }
  544. }
  545. }
  546. /**
  547. * Get the role of this object.
  548. *
  549. * @return an instance of AccessibleRole describing the role of the
  550. * object
  551. * @see AccessibleRole
  552. */
  553. public AccessibleRole getAccessibleRole() {
  554. return AccessibleRole.FRAME;
  555. }
  556. /**
  557. * Get the state of this object.
  558. *
  559. * @return an instance of AccessibleStateSet containing the current
  560. * state set of the object
  561. * @see AccessibleState
  562. */
  563. public AccessibleStateSet getAccessibleStateSet() {
  564. AccessibleStateSet states = SwingUtilities.getAccessibleStateSet(JFrame.this);
  565. if (isResizable()) {
  566. states.add(AccessibleState.RESIZABLE);
  567. }
  568. if (getFocusOwner() != null) {
  569. states.add(AccessibleState.ACTIVE);
  570. }
  571. // FIXME: [[[WDW - should also return ICONIFIED and ICONIFIABLE
  572. // if we can ever figure these out]]]
  573. return states;
  574. }
  575. /**
  576. * Get the Accessible parent of this object. If the parent of this
  577. * object implements Accessible, this method should simply return
  578. * getParent().
  579. *
  580. * @return the Accessible parent of this object -- can be null if this
  581. * object does not have an Accessible parent
  582. */
  583. public Accessible getAccessibleParent() {
  584. if (accessibleParent != null) {
  585. return accessibleParent;
  586. } else {
  587. Container parent = getParent();
  588. if (parent instanceof Accessible) {
  589. return (Accessible) parent;
  590. }
  591. }
  592. return null;
  593. }
  594. /**
  595. * Get the index of this object in its accessible parent.
  596. *
  597. * @return the index of this object in its parent; -1 if this
  598. * object does not have an accessible parent.
  599. * @see #getAccessibleParent
  600. */
  601. public int getAccessibleIndexInParent() {
  602. return SwingUtilities.getAccessibleIndexInParent(JFrame.this);
  603. }
  604. /**
  605. * Returns the number of accessible children in the object. If all
  606. * of the children of this object implement Accessible, than this
  607. * method should return the number of children of this object.
  608. *
  609. * @return the number of accessible children in the object.
  610. */
  611. public int getAccessibleChildrenCount() {
  612. return SwingUtilities.getAccessibleChildrenCount(JFrame.this);
  613. }
  614. /**
  615. * Return the nth Accessible child of the object.
  616. *
  617. * @param i zero-based index of child
  618. * @return the nth Accessible child of the object
  619. */
  620. public Accessible getAccessibleChild(int i) {
  621. return SwingUtilities.getAccessibleChild(JFrame.this,i);
  622. }
  623. /**
  624. * Return the locale of this object.
  625. *
  626. * @return the locale of this object
  627. */
  628. public Locale getLocale() {
  629. return JFrame.this.getLocale();
  630. }
  631. /**
  632. * Get the AccessibleComponent associated with this object if one
  633. * exists. Otherwise return null.
  634. */
  635. public AccessibleComponent getAccessibleComponent() {
  636. return this;
  637. }
  638. // AccessibleComponent methods
  639. //
  640. /**
  641. * Get the background color of this object.
  642. *
  643. * @return the background color, if supported, of the object;
  644. * otherwise, null
  645. */
  646. public Color getBackground() {
  647. return JFrame.this.getBackground();
  648. }
  649. /**
  650. * Set the background color of this object.
  651. *
  652. * @param c the new Color for the background
  653. */
  654. public void setBackground(Color c) {
  655. JFrame.this.setBackground(c);
  656. }
  657. /**
  658. * Get the foreground color of this object.
  659. *
  660. * @return the foreground color, if supported, of the object;
  661. * otherwise, null
  662. */
  663. public Color getForeground() {
  664. return JFrame.this.getForeground();
  665. }
  666. /**
  667. * Set the foreground color of this object.
  668. *
  669. * @param c the new Color for the foreground
  670. */
  671. public void setForeground(Color c) {
  672. JFrame.this.setForeground(c);
  673. }
  674. /**
  675. * Get the Cursor of this object.
  676. *
  677. * @return the Cursor, if supported, of the object; otherwise, null
  678. */
  679. public Cursor getCursor() {
  680. return JFrame.this.getCursor();
  681. }
  682. /**
  683. * Set the Cursor of this object.
  684. *
  685. * @param c the new Cursor for the object
  686. */
  687. public void setCursor(Cursor cursor) {
  688. JFrame.this.setCursor(cursor);
  689. }
  690. /**
  691. * Get the Font of this object.
  692. *
  693. * @return the Font,if supported, for the object; otherwise, null
  694. */
  695. public Font getFont() {
  696. return JFrame.this.getFont();
  697. }
  698. /**
  699. * Set the Font of this object.
  700. *
  701. * @param f the new Font for the object
  702. */
  703. public void setFont(Font f) {
  704. JFrame.this.setFont(f);
  705. }
  706. /**
  707. * Get the FontMetrics of this object.
  708. *
  709. * @param f the Font
  710. * @return the FontMetrics, if supported, the object; otherwise, null
  711. * @see #getFont
  712. */
  713. public FontMetrics getFontMetrics(Font f) {
  714. return JFrame.this.getFontMetrics(f);
  715. }
  716. /**
  717. * Determine if the object is enabled.
  718. *
  719. * @return true if object is enabled; otherwise, false
  720. */
  721. public boolean isEnabled() {
  722. return JFrame.this.isEnabled();
  723. }
  724. /**
  725. * Set the enabled state of the object.
  726. *
  727. * @param b if true, enables this object; otherwise, disables it
  728. */
  729. public void setEnabled(boolean b) {
  730. JFrame.this.setEnabled(b);
  731. }
  732. /**
  733. * Determine if the object is visible. Note: this means that the
  734. * object intends to be visible; however, it may not in fact be
  735. * showing on the screen because one of the objects that this object
  736. * is contained by is not visible. To determine if an object is
  737. * showing on the screen, use isShowing().
  738. *
  739. * @return true if object is visible; otherwise, false
  740. */
  741. public boolean isVisible() {
  742. return JFrame.this.isVisible();
  743. }
  744. /**
  745. * Set the visible state of the object.
  746. *
  747. * @param b if true, shows this object; otherwise, hides it
  748. */
  749. public void setVisible(boolean b) {
  750. JFrame.this.setVisible(b);
  751. }
  752. /**
  753. * Determine if the object is showing. This is determined by checking
  754. * the visibility of the object and ancestors of the object. Note:
  755. * this will return true even if the object is obscured by another
  756. * (for example, it happens to be underneath a menu that was pulled
  757. * down).
  758. *
  759. * @return true if object is showing; otherwise, false
  760. */
  761. public boolean isShowing() {
  762. return JFrame.this.isShowing();
  763. }
  764. /**
  765. * Checks whether the specified point is within this object's bounds,
  766. * where the point's x and y coordinates are defined to be relative to
  767. * the coordinate system of the object.
  768. *
  769. * @param p the Point relative to the coordinate system of the object
  770. * @return true if object contains Point; otherwise false
  771. */
  772. public boolean contains(Point p) {
  773. return JFrame.this.contains(p);
  774. }
  775. /**
  776. * Returns the location of the object on the screen.
  777. *
  778. * @return location of object on screen -- can be null if this object
  779. * is not on the screen
  780. */
  781. public Point getLocationOnScreen() {
  782. return JFrame.this.getLocationOnScreen();
  783. }
  784. /**
  785. * Gets the location of the object relative to the parent in the form
  786. * of a point specifying the object's top-left corner in the screen's
  787. * coordinate space.
  788. *
  789. * @return An instance of Point representing the top-left corner of
  790. * the objects's bounds in the coordinate space of the screen; null if
  791. * this object or its parent are not on the screen
  792. */
  793. public Point getLocation() {
  794. return JFrame.this.getLocation();
  795. }
  796. /**
  797. * Sets the location of the object relative to the parent.
  798. */
  799. public void setLocation(Point p) {
  800. JFrame.this.setLocation(p);
  801. }
  802. /**
  803. * Gets the bounds of this object in the form of a Rectangle object.
  804. * The bounds specify this object's width, height, and location
  805. * relative to its parent.
  806. *
  807. * @return A rectangle indicating this component's bounds; null if
  808. * this object is not on the screen.
  809. */
  810. public Rectangle getBounds() {
  811. return JFrame.this.getBounds();
  812. }
  813. /**
  814. * Sets the bounds of this object in the form of a Rectangle object.
  815. * The bounds specify this object's width, height, and location
  816. * relative to its parent.
  817. *
  818. * @param A rectangle indicating this component's bounds
  819. */
  820. public void setBounds(Rectangle r) {
  821. JFrame.this.setBounds(r);
  822. }
  823. /**
  824. * Returns the size of this object in the form of a Dimension object.
  825. * The height field of the Dimension object contains this objects's
  826. * height, and the width field of the Dimension object contains this
  827. * object's width.
  828. *
  829. * @return A Dimension object that indicates the size of this
  830. * component; null if this object is not on the screen
  831. */
  832. public Dimension getSize() {
  833. return JFrame.this.getSize();
  834. }
  835. /**
  836. * Resizes this object so that it has width width and height.
  837. *
  838. * @param d - The dimension specifying the new size of the object.
  839. */
  840. public void setSize(Dimension d) {
  841. JFrame.this.setSize(d);
  842. }
  843. /**
  844. * Returns the Accessible child, if one exists, contained at the local
  845. * coordinate Point.
  846. *
  847. * @param p The point defining the top-left corner of the Accessible,
  848. * given in the coordinate space of the object's parent.
  849. * @return the Accessible, if it exists, at the specified location;
  850. * else null
  851. */
  852. public Accessible getAccessibleAt(Point p) {
  853. Accessible a;
  854. AccessibleContext ac;
  855. AccessibleComponent acmp;
  856. Point location;
  857. int nchildren = getAccessibleChildrenCount();
  858. for (int i=0; i < nchildren; i++) {
  859. a = getAccessibleChild(i);
  860. if ((a != null)) {
  861. ac = a.getAccessibleContext();
  862. if (ac != null) {
  863. acmp = ac.getAccessibleComponent();
  864. if ((acmp != null) && (acmp.isShowing())) {
  865. location = acmp.getLocation();
  866. Point np = new Point(p.x-location.x, p.y-location.y)
  867. ;
  868. if (acmp.contains(np)){
  869. return a;
  870. }
  871. }
  872. }
  873. }
  874. }
  875. return (Accessible) JFrame.this;
  876. // return SwingUtilities.getAccessibleAt(JFrame.this,p);
  877. }
  878. /**
  879. * Returns whether this object can accept focus or not.
  880. *
  881. * @return true if object can accept focus; otherwise false
  882. */
  883. public boolean isFocusTraversable() {
  884. return JFrame.this.isFocusTraversable();
  885. }
  886. /**
  887. * Requests focus for this object.
  888. */
  889. public void requestFocus() {
  890. JFrame.this.requestFocus();
  891. }
  892. /**
  893. * Adds the specified focus listener to receive focus events from this
  894. * component.
  895. *
  896. * @param l the focus listener
  897. */
  898. public void addFocusListener(FocusListener l) {
  899. JFrame.this.addFocusListener(l);
  900. }
  901. /**
  902. * Removes the specified focus listener so it no longer receives focus
  903. * events from this component.
  904. *
  905. * @param l the focus listener
  906. */
  907. public void removeFocusListener(FocusListener l) {
  908. JFrame.this.removeFocusListener(l);
  909. }
  910. } // inner class AccessibleJFrame
  911. }