1. /*
  2. * @(#)Component.java 1.239 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.io.PrintStream;
  9. import java.io.PrintWriter;
  10. import java.util.Vector;
  11. import java.util.Locale;
  12. import java.awt.peer.ComponentPeer;
  13. import java.awt.image.ImageObserver;
  14. import java.awt.image.ImageProducer;
  15. import java.awt.image.ColorModel;
  16. import java.awt.event.*;
  17. import java.awt.datatransfer.Transferable;
  18. import java.awt.dnd.DnDConstants;
  19. import java.awt.dnd.DragSource;
  20. import java.awt.dnd.DragSourceContext;
  21. import java.awt.dnd.DragSourceListener;
  22. import java.awt.dnd.InvalidDnDOperationException;
  23. import java.io.Serializable;
  24. import java.io.ObjectOutputStream;
  25. import java.io.ObjectInputStream;
  26. import java.io.IOException;
  27. import java.beans.PropertyChangeListener;
  28. import java.awt.event.InputMethodListener;
  29. import java.awt.event.InputMethodEvent;
  30. import java.awt.im.InputContext;
  31. import java.awt.im.InputMethodRequests;
  32. import java.awt.dnd.DropTarget;
  33. import sun.security.action.GetPropertyAction;
  34. import sun.awt.AppContext;
  35. import sun.awt.SunToolkit;
  36. import sun.awt.ConstrainableGraphics;
  37. /**
  38. * A <em>component</em> is an object having a graphical representation
  39. * that can be displayed on the screen and that can interact with the
  40. * user. Examples of components are the buttons, checkboxes, and scrollbars
  41. * of a typical graphical user interface. <p>
  42. * The <code>Component</code> class is the abstract superclass of
  43. * the nonmenu-related Abstract Window Toolkit components. Class
  44. * <code>Component</code> can also be extended directly to create a
  45. * lightweight component. A lightweight component is a component that is
  46. * not associated with a native opaque window.
  47. *
  48. * @version 1.226, 12/15/98
  49. * @author Arthur van Hoff
  50. * @author Sami Shaio
  51. */
  52. public abstract class Component implements ImageObserver, MenuContainer,
  53. Serializable
  54. {
  55. /**
  56. * The peer of the component. The peer implements the component's
  57. * behaviour. The peer is set when the Component is added to a
  58. * container that also is a peer.
  59. * @see #addNotify
  60. * @see #removeNotify
  61. */
  62. transient ComponentPeer peer;
  63. /**
  64. * The parent of the object. It may be null for top-level components.
  65. * @see #getParent
  66. */
  67. transient Container parent;
  68. /**
  69. * The AppContext of the component. This is set in the constructor
  70. * and never changes.
  71. */
  72. transient AppContext appContext;
  73. /**
  74. * The x position of the component in the parent's coordinate system.
  75. *
  76. * @serial
  77. * @see #getLocation
  78. */
  79. int x;
  80. /**
  81. * The y position of the component in the parent's coordinate system.
  82. *
  83. * @serial
  84. * @see #getLocation
  85. */
  86. int y;
  87. /**
  88. * The width of the component.
  89. *
  90. * @serial
  91. * @see #getSize
  92. */
  93. int width;
  94. /**
  95. * The height of the component.
  96. *
  97. * @serial
  98. * @see #getSize
  99. */
  100. int height;
  101. /**
  102. * The foreground color for this component.
  103. * foreground color can be null.
  104. *
  105. * @serial
  106. * @see #getForeground
  107. * @see #setForeground
  108. */
  109. Color foreground;
  110. /**
  111. * The background color for this component.
  112. * background can be null.
  113. *
  114. * @serial
  115. * @see #getBackground
  116. * @see #setBackground
  117. */
  118. Color background;
  119. /**
  120. * The font used by this component.
  121. * The font can be null.
  122. *
  123. * @serial
  124. * @see #getFont
  125. * @see #setFont
  126. */
  127. Font font;
  128. /**
  129. * The font which the peer is currently using. (null if no peer exists.)
  130. */
  131. Font peerFont;
  132. /**
  133. * The cursor displayed when pointer is over this component.
  134. * cursor must always be a non-null cursor image.
  135. *
  136. * @serial
  137. * @see #getCursor
  138. * @see #setCursor
  139. */
  140. Cursor cursor;
  141. /**
  142. * The locale for the component.
  143. *
  144. * @serial
  145. * @see #getLocale
  146. * @see #setLocale
  147. */
  148. Locale locale;
  149. /**
  150. * True when the object is visible. An object that is not
  151. * visible is not drawn on the screen.
  152. *
  153. * @serial
  154. * @see #isVisible
  155. * @see #setVisible
  156. */
  157. boolean visible = true;
  158. /**
  159. * True when the object is enabled. An object that is not
  160. * enabled does not interact with the user.
  161. *
  162. * @serial
  163. * @see #isEnabled
  164. * @see #setEnabled
  165. */
  166. boolean enabled = true;
  167. /**
  168. * True when the object is valid. An invalid object needs to
  169. * be layed out. This flag is set to false when the object
  170. * size is changed.
  171. *
  172. * @serial
  173. * @see #isValid
  174. * @see #validate
  175. * @see #invalidate
  176. */
  177. boolean valid = false;
  178. /**
  179. * The DropTarget associated with this Component.
  180. *
  181. * @since JDK 1.2
  182. * @serial
  183. * @see #setDropTarget
  184. * @see #getDropTarget
  185. */
  186. DropTarget dropTarget;
  187. /**
  188. * True if this component has enabled focus events and currently
  189. * has the focus.
  190. *
  191. * @serial
  192. * @see #hasFocus
  193. * @see #processFocusEvent
  194. */
  195. boolean hasFocus = false;
  196. /**
  197. * @serial
  198. * @see add()
  199. */
  200. Vector popups;
  201. /**
  202. * A components name.
  203. * This field can be null.
  204. *
  205. * @serial
  206. * @see getName()
  207. * @see setName()
  208. */
  209. private String name;
  210. /**
  211. * A bool to determine whether the name has
  212. * been set explicitly. nameExplicitlySet will
  213. * be false if the name has not been set and
  214. * true if it has.
  215. *
  216. * @serial
  217. * @see getName()
  218. * @see setName()
  219. */
  220. private boolean nameExplicitlySet = false;
  221. /**
  222. * The locking object for AWT component-tree and layout operations.
  223. *
  224. * @see #getTreeLock
  225. */
  226. static final Object LOCK = new AWTTreeLock();
  227. static class AWTTreeLock {}
  228. /**
  229. * Internal, cached size information.
  230. * (This field perhaps should have been transient).
  231. *
  232. * @serial
  233. */
  234. Dimension minSize;
  235. /** Internal, cached size information
  236. * (This field perhaps should have been transient).
  237. *
  238. * @serial
  239. */
  240. Dimension prefSize;
  241. /**
  242. * The orientation for this component.
  243. * @see #getComponentOrientation
  244. * @see #setComponentOrientation(java.awt.ComponentOrientation)
  245. */
  246. transient ComponentOrientation componentOrientation
  247. = ComponentOrientation.UNKNOWN;
  248. /**
  249. * newEventsOnly will be true if the event is
  250. * one of the event types enabled for the component.
  251. * It will then allow for normal processing to
  252. * continue. If it is false the event is passed
  253. * to the components parent and up the ancestor
  254. * tree until the event has been consumed.
  255. *
  256. * @serial
  257. * @see dispatchEvent()
  258. */
  259. boolean newEventsOnly = false;
  260. transient ComponentListener componentListener;
  261. transient FocusListener focusListener;
  262. transient KeyListener keyListener;
  263. transient MouseListener mouseListener;
  264. transient MouseMotionListener mouseMotionListener;
  265. transient InputMethodListener inputMethodListener;
  266. /** Internal, constants for serialization */
  267. final static String actionListenerK = "actionL";
  268. final static String adjustmentListenerK = "adjustmentL";
  269. final static String componentListenerK = "componentL";
  270. final static String containerListenerK = "containerL";
  271. final static String focusListenerK = "focusL";
  272. final static String itemListenerK = "itemL";
  273. final static String keyListenerK = "keyL";
  274. final static String mouseListenerK = "mouseL";
  275. final static String mouseMotionListenerK = "mouseMotionL";
  276. final static String textListenerK = "textL";
  277. final static String ownedWindowK = "ownedL";
  278. final static String windowListenerK = "windowL";
  279. final static String inputMethodListenerK = "inputMethodL";
  280. /**
  281. * The eventMask is ONLY set by subclasses via enableEvents.
  282. * The mask should NOT be set when listeners are registered
  283. * so that we can distinguish the difference between when
  284. * listeners request events and subclasses request them.
  285. * One bit is used to indicate whether input methods are
  286. * enabled; this bit is set by enableInputMethods and is
  287. * on by default.
  288. *
  289. * @serial
  290. * @see enableInputMethods()
  291. */
  292. long eventMask = AWTEvent.INPUT_METHODS_ENABLED_MASK;
  293. // enable for assertion checking
  294. private final static boolean assert = false;
  295. /**
  296. * Static properties for incremental drawing.
  297. * @see #imageUpdate
  298. */
  299. static boolean isInc;
  300. static int incRate;
  301. static {
  302. /* ensure that the necessary native libraries are loaded */
  303. Toolkit.loadLibraries();
  304. /* initialize JNI field and method ids */
  305. initIDs();
  306. String s = (String) java.security.AccessController.doPrivileged(
  307. new GetPropertyAction("awt.image.incrementaldraw"));
  308. isInc = (s == null || s.equals("true"));
  309. s = (String) java.security.AccessController.doPrivileged(
  310. new GetPropertyAction("awt.image.redrawrate"));
  311. incRate = (s != null) ? Integer.parseInt(s) : 100;
  312. }
  313. /**
  314. * Ease-of-use constant for <code>getAlignmentY()</code>. Specifies an
  315. * alignment to the top of the component.
  316. * @see #getAlignmentY
  317. */
  318. public static final float TOP_ALIGNMENT = 0.0f;
  319. /**
  320. * Ease-of-use constant for <code>getAlignmentY</code> and
  321. * <code>getAlignmentX</code>. Specifies an alignment to
  322. * the center of the component
  323. * @see #getAlignmentX
  324. * @see #getAlignmentY
  325. */
  326. public static final float CENTER_ALIGNMENT = 0.5f;
  327. /**
  328. * Ease-of-use constant for <code>getAlignmentY</code>. Specifies an
  329. * alignment to the bottom of the component.
  330. * @see #getAlignmentY
  331. */
  332. public static final float BOTTOM_ALIGNMENT = 1.0f;
  333. /**
  334. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  335. * alignment to the left side of the component.
  336. * @see #getAlignmentX
  337. */
  338. public static final float LEFT_ALIGNMENT = 0.0f;
  339. /**
  340. * Ease-of-use constant for <code>getAlignmentX</code>. Specifies an
  341. * alignment to the right side of the component.
  342. * @see #getAlignmentX
  343. */
  344. public static final float RIGHT_ALIGNMENT = 1.0f;
  345. /*
  346. * JDK 1.1 serialVersionUID
  347. */
  348. private static final long serialVersionUID = -7644114512714619750L;
  349. /**
  350. * If any PropertyChangeListeners have been registered, the
  351. * changeSupport field describes them.
  352. *
  353. * @serial
  354. * @since JDK 1.2
  355. * @see addPropertyChangeListener()
  356. * @see removePropertyChangeListener()
  357. * @see firePropertyChange()
  358. */
  359. private java.beans.PropertyChangeSupport changeSupport;
  360. boolean isPacked = false;
  361. /**
  362. * Constructs a new component. Class <code>Component</code> can be
  363. * extended directly to create a lightweight component that does not
  364. * utilize an opaque native window. A lightweight component must be
  365. * hosted by a native container somewhere higher up in the component
  366. * tree (for example, by a <code>Frame</code> object).
  367. */
  368. protected Component() {
  369. appContext = AppContext.getAppContext();
  370. SunToolkit.insertTargetMapping(this, appContext);
  371. }
  372. /**
  373. * Construct a name for this component. Called by getName() when the
  374. * name is null.
  375. */
  376. String constructComponentName() {
  377. return null; // For strict compliance with prior JDKs, a Component
  378. // that doesn't set its name should return null from
  379. // getName()
  380. }
  381. /**
  382. * Gets the name of the component.
  383. * @return This component's name.
  384. * @see #setName
  385. * @since JDK1.1
  386. */
  387. public String getName() {
  388. if (name == null && !nameExplicitlySet) {
  389. synchronized(this) {
  390. if (name == null && !nameExplicitlySet)
  391. name = constructComponentName();
  392. }
  393. }
  394. return name;
  395. }
  396. /**
  397. * Sets the name of the component to the specified string.
  398. * @param name The string that is to be this
  399. * component's name.
  400. * @see #getName
  401. * @since JDK1.1
  402. */
  403. public void setName(String name) {
  404. synchronized(this) {
  405. this.name = name;
  406. nameExplicitlySet = true;
  407. }
  408. }
  409. /**
  410. * Gets the parent of this component.
  411. * @return The parent container of this component.
  412. * @since JDK1.0
  413. */
  414. public Container getParent() {
  415. return getParent_NoClientCode();
  416. }
  417. // NOTE: This method may be called by privileged threads.
  418. // This functionality is implemented in a package-private method
  419. // to insure that it cannot be overridden by client subclasses.
  420. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  421. final Container getParent_NoClientCode() {
  422. return parent;
  423. }
  424. /**
  425. * @deprecated As of JDK version 1.1,
  426. * programs should not directly manipulate peers.
  427. * replaced by <code>boolean isDisplayable()</code>.
  428. */
  429. public ComponentPeer getPeer() {
  430. return peer;
  431. }
  432. /**
  433. * Associate a DropTarget with this Component.
  434. *
  435. * @param dt The DropTarget
  436. */
  437. public synchronized void setDropTarget(DropTarget dt) {
  438. if (dt == dropTarget || (dropTarget != null && dropTarget.equals(dt)))
  439. return;
  440. DropTarget old;
  441. if ((old = dropTarget) != null) {
  442. if (peer != null) dropTarget.removeNotify(peer);
  443. DropTarget t = dropTarget;
  444. dropTarget = null;
  445. try {
  446. t.setComponent(null);
  447. } catch (IllegalArgumentException iae) {
  448. // ignore it.
  449. }
  450. }
  451. // if we have a new one, and we have a peer, add it!
  452. if ((dropTarget = dt) != null) {
  453. try {
  454. dropTarget.setComponent(this);
  455. if (peer != null) dropTarget.addNotify(peer);
  456. } catch (IllegalArgumentException iae) {
  457. if (old != null) {
  458. try {
  459. old.setComponent(this);
  460. if (peer != null) dropTarget.addNotify(peer);
  461. } catch (IllegalArgumentException iae1) {
  462. // ignore it!
  463. }
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * Get the DropTarget associated with this Component
  470. */
  471. public synchronized DropTarget getDropTarget() { return dropTarget; }
  472. /**
  473. * Gets the locking object for AWT component-tree and layout
  474. * Gets this component's locking object (the object that owns the thread
  475. * sychronization monitor) for AWT component-tree and layout
  476. * operations.
  477. * @return This component's locking object.
  478. */
  479. public final Object getTreeLock() {
  480. return LOCK;
  481. }
  482. /**
  483. * Gets the toolkit of this component. Note that
  484. * the frame that contains a component controls which
  485. * toolkit is used by that component. Therefore if the component
  486. * is moved from one frame to another, the toolkit it uses may change.
  487. * @return The toolkit of this component.
  488. * @since JDK1.0
  489. */
  490. public Toolkit getToolkit() {
  491. return getToolkitImpl();
  492. }
  493. /*
  494. * This is called by the native code, so client code can't
  495. * be called on the toolkit thread.
  496. */
  497. final Toolkit getToolkitImpl() {
  498. ComponentPeer peer = this.peer;
  499. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)){
  500. return peer.getToolkit();
  501. }
  502. Container parent = this.parent;
  503. if (parent != null) {
  504. return parent.getToolkitImpl();
  505. }
  506. return Toolkit.getDefaultToolkit();
  507. }
  508. /**
  509. * Determines whether this component is valid. A component is valid
  510. * when it is correctly sized and positioned within its parent
  511. * container and all its children are also valid. Components are
  512. * invalidated when they are first shown on the screen.
  513. * @return <code>true</code> if the component is valid; <code>false</code>
  514. * otherwise.
  515. * @see #validate
  516. * @see #invalidate
  517. * @since JDK1.0
  518. */
  519. public boolean isValid() {
  520. return (peer != null) && valid;
  521. }
  522. /**
  523. * Determines whether this component is displayable. A component is
  524. * displayable when it is connected to a native screen resource.
  525. * <p>
  526. * A component is made displayable either when it is added to
  527. * a displayable containment hierarchy or when its containment
  528. * hierarchy is made displayable.
  529. * A containment hierarchy is made displayable when its ancestor
  530. * window is either packed or made visible.
  531. * <p>
  532. * A component is made undisplayable either when it is removed from
  533. * a displayable containment hierarchy or when its containment hierarchy
  534. * is made undisplayable. A containment hierarchy is made
  535. * undisplayable when its ancestor window is disposed.
  536. *
  537. * @return <code>true</code> if the component is displayable;
  538. * <code>false</code> otherwise.
  539. * @see java.awt.Container#add(java.awt.Component)
  540. * @see java.awt.Window#pack
  541. * @see java.awt.Window#show
  542. * @see java.awt.Container#remove(java.awt.Component)
  543. * @see java.awt.Window#dispose
  544. * @since JDK1.2
  545. */
  546. public boolean isDisplayable() {
  547. return getPeer() != null;
  548. }
  549. /**
  550. * Determines whether this component should be visible when its
  551. * parent is visible. Components are
  552. * initially visible, with the exception of top level components such
  553. * as <code>Frame</code> objects.
  554. * @return <code>true</code> if the component is visible;
  555. * <code>false</code> otherwise.
  556. * @see #setVisible
  557. * @since JDK1.0
  558. */
  559. public boolean isVisible() {
  560. return visible;
  561. }
  562. /**
  563. * Determines whether this component is showing on screen. This means
  564. * that the component must be visible, and it must be in a container
  565. * that is visible and showing.
  566. * @return <code>true</code> if the component is showing;
  567. * <code>false</code> otherwise.
  568. * @see #setVisible
  569. * @since JDK1.0
  570. */
  571. public boolean isShowing() {
  572. if (visible && (peer != null)) {
  573. Container parent = this.parent;
  574. return (parent == null) || parent.isShowing();
  575. }
  576. return false;
  577. }
  578. /**
  579. * Determines whether this component is enabled. An enabled component
  580. * can respond to user input and generate events. Components are
  581. * enabled initially by default. A component may be enabled or disabled by
  582. * calling its <code>setEnabled</code> method.
  583. * @return <code>true</code> if the component is enabled;
  584. * <code>false</code> otherwise.
  585. * @see #setEnabled
  586. * @since JDK1.0
  587. */
  588. public boolean isEnabled() {
  589. return isEnabledImpl();
  590. }
  591. /*
  592. * This is called by the native code, so client code can't
  593. * be called on the toolkit thread.
  594. */
  595. final boolean isEnabledImpl() {
  596. return enabled;
  597. }
  598. /**
  599. * Enables or disables this component, depending on the value of the
  600. * parameter <code>b</code>. An enabled component can respond to user
  601. * input and generate events. Components are enabled initially by default.
  602. * @param b If <code>true</code>, this component is
  603. * enabled; otherwise this component is disabled.
  604. * @see #isEnabled
  605. * @since JDK1.1
  606. */
  607. public void setEnabled(boolean b) {
  608. enable(b);
  609. }
  610. /**
  611. * @deprecated As of JDK version 1.1,
  612. * replaced by <code>setEnabled(boolean)</code>.
  613. */
  614. public void enable() {
  615. if (enabled != true) {
  616. synchronized (getTreeLock()) {
  617. enabled = true;
  618. ComponentPeer peer = this.peer;
  619. if (peer != null) {
  620. peer.enable();
  621. }
  622. }
  623. }
  624. }
  625. /**
  626. * @deprecated As of JDK version 1.1,
  627. * replaced by <code>setEnabled(boolean)</code>.
  628. */
  629. public void enable(boolean b) {
  630. if (b) {
  631. enable();
  632. } else {
  633. disable();
  634. }
  635. }
  636. /**
  637. * @deprecated As of JDK version 1.1,
  638. * replaced by <code>setEnabled(boolean)</code>.
  639. */
  640. public void disable() {
  641. if (enabled != false) {
  642. synchronized (getTreeLock()) {
  643. enabled = false;
  644. ComponentPeer peer = this.peer;
  645. if (peer != null) {
  646. peer.disable();
  647. }
  648. }
  649. }
  650. }
  651. /**
  652. * Returns true if this component is painted to an offscreen image
  653. * ("buffer") that's copied to the screen later. Component
  654. * subclasses that support double buffering should override this
  655. * method to return true if double buffering is enabled.
  656. *
  657. * @return false by default
  658. */
  659. public boolean isDoubleBuffered() {
  660. return false;
  661. }
  662. /**
  663. * Enables or disables input method support for this component. If input
  664. * method support is enabled and the component also processes key events,
  665. * incoming events are offered to
  666. * the current input method and will only be processed by the component or
  667. * dispatched to its listeners if the input method does not consume them.
  668. * By default, input method support is enabled.
  669. *
  670. * @param enable true to enable, false to disable.
  671. * @see java.awt.Component#processKeyEvent
  672. * @since JDK1.2
  673. */
  674. public void enableInputMethods(boolean enable) {
  675. if (enable) {
  676. if ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0)
  677. return;
  678. // If this component already has focus, then activate the
  679. // input method by dispatching a synthesized focus gained
  680. // event.
  681. if (hasFocus() == true) {
  682. InputContext inputContext = getInputContext();
  683. if (inputContext != null) {
  684. FocusEvent focusGainedEvent = new FocusEvent(this,
  685. FocusEvent.FOCUS_GAINED);
  686. inputContext.dispatchEvent(focusGainedEvent);
  687. }
  688. }
  689. eventMask |= AWTEvent.INPUT_METHODS_ENABLED_MASK;
  690. } else {
  691. if (areInputMethodsEnabled()) {
  692. InputContext inputContext = getInputContext();
  693. if (inputContext != null) {
  694. inputContext.endComposition();
  695. inputContext.removeNotify(this);
  696. }
  697. }
  698. eventMask &= ~AWTEvent.INPUT_METHODS_ENABLED_MASK;
  699. }
  700. }
  701. /**
  702. * Shows or hides this component depending on the value of parameter
  703. * <code>b</code>.
  704. * @param b If <code>true</code>, shows this component;
  705. * otherwise, hides this component.
  706. * @see #isVisible
  707. * @since JDK1.1
  708. */
  709. public void setVisible(boolean b) {
  710. show(b);
  711. }
  712. /**
  713. * @deprecated As of JDK version 1.1,
  714. * replaced by <code>setVisible(boolean)</code>.
  715. */
  716. public void show() {
  717. if (visible != true) {
  718. synchronized (getTreeLock()) {
  719. visible = true;
  720. ComponentPeer peer = this.peer;
  721. if (peer != null) {
  722. peer.show();
  723. if (peer instanceof java.awt.peer.LightweightPeer) {
  724. repaint();
  725. }
  726. }
  727. if (componentListener != null ||
  728. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  729. ComponentEvent e = new ComponentEvent(this,
  730. ComponentEvent.COMPONENT_SHOWN);
  731. Toolkit.getEventQueue().postEvent(e);
  732. }
  733. }
  734. Container parent = this.parent;
  735. if (parent != null) {
  736. parent.invalidate();
  737. }
  738. }
  739. }
  740. /**
  741. * @deprecated As of JDK version 1.1,
  742. * replaced by <code>setVisible(boolean)</code>.
  743. */
  744. public void show(boolean b) {
  745. if (b) {
  746. show();
  747. } else {
  748. hide();
  749. }
  750. }
  751. /**
  752. * @deprecated As of JDK version 1.1,
  753. * replaced by <code>setVisible(boolean)</code>.
  754. */
  755. public void hide() {
  756. if (visible != false) {
  757. synchronized (getTreeLock()) {
  758. visible = false;
  759. ComponentPeer peer = this.peer;
  760. if (peer != null) {
  761. peer.hide();
  762. if (peer instanceof java.awt.peer.LightweightPeer) {
  763. repaint();
  764. }
  765. }
  766. if (componentListener != null ||
  767. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  768. ComponentEvent e = new ComponentEvent(this,
  769. ComponentEvent.COMPONENT_HIDDEN);
  770. Toolkit.getEventQueue().postEvent(e);
  771. }
  772. }
  773. Container parent = this.parent;
  774. if (parent != null) {
  775. parent.invalidate();
  776. }
  777. }
  778. }
  779. /**
  780. * Gets the foreground color of this component.
  781. * @return This component's foreground color. If this component does
  782. * not have a foreground color, the foreground color of its parent
  783. * is returned.
  784. * @see #java.awt.Component#setForeground(java.awt.Color)
  785. * @since JDK1.0
  786. */
  787. public Color getForeground() {
  788. Color foreground = this.foreground;
  789. if (foreground != null) {
  790. return foreground;
  791. }
  792. Container parent = this.parent;
  793. return (parent != null) ? parent.getForeground() : null;
  794. }
  795. /**
  796. * Sets the foreground color of this component.
  797. * @param c The color to become this component's
  798. * foreground color.
  799. * If this parameter is null then this component will inherit
  800. * the foreground color of its parent.
  801. * @see #getForeground
  802. * @since JDK1.0
  803. */
  804. public void setForeground(Color c) {
  805. Color oldColor = foreground;
  806. ComponentPeer peer = this.peer;
  807. foreground = c;
  808. if (peer != null) {
  809. c = getForeground();
  810. if (c != null) {
  811. peer.setForeground(c);
  812. }
  813. }
  814. // This is a bound property, so report the change to
  815. // any registered listeners. (Cheap if there are none.)
  816. firePropertyChange("foreground", oldColor, c);
  817. }
  818. /**
  819. * Gets the background color of this component.
  820. * @return This component's background color. If this component does
  821. * not have a background color, the background color of its parent
  822. * is returned.
  823. * @see java.awt.Component#setBackground(java.awt.Color)
  824. * @since JDK1.0
  825. */
  826. public Color getBackground() {
  827. Color background = this.background;
  828. if (background != null) {
  829. return background;
  830. }
  831. Container parent = this.parent;
  832. return (parent != null) ? parent.getBackground() : null;
  833. }
  834. /**
  835. * Sets the background color of this component.
  836. * @param c The color to become this component's color.
  837. * If this parameter is null then this component will inherit
  838. * the background color of its parent.
  839. * background color.
  840. * @see #getBackground
  841. * @since JDK1.0
  842. */
  843. public void setBackground(Color c) {
  844. Color oldColor = background;
  845. ComponentPeer peer = this.peer;
  846. background = c;
  847. if (peer != null) {
  848. c = getBackground();
  849. if (c != null) {
  850. peer.setBackground(c);
  851. }
  852. }
  853. // This is a bound property, so report the change to
  854. // any registered listeners. (Cheap if there are none.)
  855. firePropertyChange("background", oldColor, c);
  856. }
  857. /**
  858. * Gets the font of this component.
  859. * @return This component's font. If a font has not been set
  860. * for this component, the font of its parent is returned.
  861. * @see #setFont
  862. * @since JDK1.0
  863. */
  864. public Font getFont() {
  865. Font font = this.font;
  866. if (font != null) {
  867. return font;
  868. }
  869. Container parent = this.parent;
  870. return (parent != null) ? parent.getFont() : null;
  871. }
  872. // NOTE: This method may be called by privileged threads.
  873. // This functionality is implemented in a package-private method
  874. // to insure that it cannot be overridden by client subclasses.
  875. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  876. final Font getFont_NoClientCode() {
  877. Font font = this.font;
  878. if (font != null) {
  879. return font;
  880. }
  881. Container parent = this.parent;
  882. return (parent != null) ? parent.getFont_NoClientCode() : null;
  883. }
  884. /**
  885. * Sets the font of this component.
  886. * @param f The font to become this component's font.
  887. * If this parameter is null then this component will inherit
  888. * the font of its parent.
  889. * @see #getFont
  890. * @since JDK1.0
  891. */
  892. public void setFont(Font f) {
  893. synchronized (this) {
  894. Font oldFont = font;
  895. ComponentPeer peer = this.peer;
  896. font = f;
  897. if (peer != null) {
  898. f = getFont();
  899. if (f != null) {
  900. peer.setFont(f);
  901. peerFont = f;
  902. }
  903. }
  904. // This is a bound property, so report the change to
  905. // any registered listeners. (Cheap if there are none.)
  906. firePropertyChange("font", oldFont, font);
  907. }
  908. // This could change the preferred size of the Component.
  909. if (valid) {
  910. invalidate();
  911. }
  912. }
  913. /**
  914. * Gets the locale of this component.
  915. * @return This component's locale. If this component does not
  916. * have a locale, the locale of its parent is returned.
  917. * @see #setLocale
  918. * @exception IllegalComponentStateException If the Component
  919. * does not have its own locale and has not yet been added to
  920. * a containment hierarchy such that the locale can be determined
  921. * from the containing parent.
  922. * @since JDK1.1
  923. */
  924. public Locale getLocale() {
  925. Locale locale = this.locale;
  926. if (locale != null) {
  927. return locale;
  928. }
  929. Container parent = this.parent;
  930. if (parent == null) {
  931. throw new IllegalComponentStateException("This component must have a parent in order to determine its locale");
  932. } else {
  933. return parent.getLocale();
  934. }
  935. }
  936. /**
  937. * Sets the locale of this component.
  938. * @param l The locale to become this component's locale.
  939. * @see #getLocale
  940. * @since JDK1.1
  941. */
  942. public void setLocale(Locale l) {
  943. locale = l;
  944. // This could change the preferred size of the Component.
  945. if (valid) {
  946. invalidate();
  947. }
  948. }
  949. /**
  950. * Gets the instance of <code>ColorModel</code> used to display
  951. * the component on the output device.
  952. * @return The color model used by this component.
  953. * @see java.awt.image.ColorModel
  954. * @see java.awt.peer.ComponentPeer#getColorModel()
  955. * @see java.awt.Toolkit#getColorModel()
  956. * @since JDK1.0
  957. */
  958. public ColorModel getColorModel() {
  959. ComponentPeer peer = this.peer;
  960. if ((peer != null) && ! (peer instanceof java.awt.peer.LightweightPeer)) {
  961. return peer.getColorModel();
  962. }
  963. return getToolkit().getColorModel();
  964. }
  965. /**
  966. * Gets the location of this component in the form of a
  967. * point specifying the component's top-left corner.
  968. * The location will be relative to the parent's coordinate space.
  969. * @return An instance of <code>Point</code> representing
  970. * the top-left corner of the component's bounds in the coordinate
  971. * space of the component's parent.
  972. * @see #setLocation
  973. * @see #getLocationOnScreen
  974. * @since JDK1.1
  975. */
  976. public Point getLocation() {
  977. return location();
  978. }
  979. /**
  980. * Gets the location of this component in the form of a point
  981. * specifying the component's top-left corner in the screen's
  982. * coordinate space.
  983. * @return An instance of <code>Point</code> representing
  984. * the top-left corner of the component's bounds in the
  985. * coordinate space of the screen.
  986. * @see #setLocation
  987. * @see #getLocation
  988. */
  989. public Point getLocationOnScreen() {
  990. synchronized (getTreeLock()) {
  991. if (peer != null && isShowing()) {
  992. if (peer instanceof java.awt.peer.LightweightPeer) {
  993. // lightweight component location needs to be translated
  994. // relative to a native component.
  995. Container host = getNativeContainer();
  996. Point pt = host.peer.getLocationOnScreen();
  997. for(Component c = this; c != host; c = c.getParent()) {
  998. pt.x += c.x;
  999. pt.y += c.y;
  1000. }
  1001. return pt;
  1002. } else {
  1003. Point pt = peer.getLocationOnScreen();
  1004. return pt;
  1005. }
  1006. } else {
  1007. throw new IllegalComponentStateException("component must be showing on the screen to determine its location");
  1008. }
  1009. }
  1010. }
  1011. /**
  1012. * @deprecated As of JDK version 1.1,
  1013. * replaced by <code>getLocation()</code>.
  1014. */
  1015. public Point location() {
  1016. return new Point(x, y);
  1017. }
  1018. /**
  1019. * Moves this component to a new location. The top-left corner of
  1020. * the new location is specified by the <code>x</code> and <code>y</code>
  1021. * parameters in the coordinate space of this component's parent.
  1022. * @param x The <i>x</i>-coordinate of the new location's
  1023. * top-left corner in the parent's coordinate space.
  1024. * @param y The <i>y</i>-coordinate of the new location's
  1025. * top-left corner in the parent's coordinate space.
  1026. * @see #getLocation
  1027. * @see #setBounds
  1028. * @since JDK1.1
  1029. */
  1030. public void setLocation(int x, int y) {
  1031. move(x, y);
  1032. }
  1033. /**
  1034. * @deprecated As of JDK version 1.1,
  1035. * replaced by <code>setLocation(int, int)</code>.
  1036. */
  1037. public void move(int x, int y) {
  1038. setBounds(x, y, width, height);
  1039. }
  1040. /**
  1041. * Moves this component to a new location. The top-left corner of
  1042. * the new location is specified by point <code>p</code>. Point
  1043. * <code>p</code> is given in the parent's coordinate space.
  1044. * @param p The point defining the top-left corner
  1045. * of the new location, given in the coordinate space of this
  1046. * component's parent.
  1047. * @see #getLocation
  1048. * @see #setBounds
  1049. * @since JDK1.1
  1050. */
  1051. public void setLocation(Point p) {
  1052. setLocation(p.x, p.y);
  1053. }
  1054. /**
  1055. * Returns the size of this component in the form of a
  1056. * <code>Dimension</code> object. The <code>height</code>
  1057. * field of the <code>Dimension</code> object contains
  1058. * this component's height, and the <code>width</code>
  1059. * field of the <code>Dimension</code> object contains
  1060. * this component's width.
  1061. * @return A <code>Dimension</code> object that indicates the
  1062. * size of this component.
  1063. * @see #setSize
  1064. * @since JDK1.1
  1065. */
  1066. public Dimension getSize() {
  1067. return size();
  1068. }
  1069. /**
  1070. * @deprecated As of JDK version 1.1,
  1071. * replaced by <code>getSize()</code>.
  1072. */
  1073. public Dimension size() {
  1074. return new Dimension(width, height);
  1075. }
  1076. /**
  1077. * Resizes this component so that it has width <code>width</code>
  1078. * and <code>height</code>.
  1079. * @param width The new width of this component in pixels.
  1080. * @param height The new height of this component in pixels.
  1081. * @see #getSize
  1082. * @see #setBounds
  1083. * @since JDK1.1
  1084. */
  1085. public void setSize(int width, int height) {
  1086. resize(width, height);
  1087. }
  1088. /**
  1089. * @deprecated As of JDK version 1.1,
  1090. * replaced by <code>setSize(int, int)</code>.
  1091. */
  1092. public void resize(int width, int height) {
  1093. setBounds(x, y, width, height);
  1094. }
  1095. /**
  1096. * Resizes this component so that it has width <code>d.width</code>
  1097. * and height <code>d.height</code>.
  1098. * @param d The dimension specifying the new size
  1099. * of this component.
  1100. * @see #setSize
  1101. * @see #setBounds
  1102. * @since JDK1.1
  1103. */
  1104. public void setSize(Dimension d) {
  1105. resize(d);
  1106. }
  1107. /**
  1108. * @deprecated As of JDK version 1.1,
  1109. * replaced by <code>setSize(Dimension)</code>.
  1110. */
  1111. public void resize(Dimension d) {
  1112. setSize(d.width, d.height);
  1113. }
  1114. /**
  1115. * Gets the bounds of this component in the form of a
  1116. * <code>Rectangle</code> object. The bounds specify this
  1117. * component's width, height, and location relative to
  1118. * its parent.
  1119. * @return A rectangle indicating this component's bounds.
  1120. * @see #setBounds
  1121. * @see #getLocation
  1122. * @see #getSize
  1123. */
  1124. public Rectangle getBounds() {
  1125. return bounds();
  1126. }
  1127. /**
  1128. * @deprecated As of JDK version 1.1,
  1129. * replaced by <code>getBounds()</code>.
  1130. */
  1131. public Rectangle bounds() {
  1132. return new Rectangle(x, y, width, height);
  1133. }
  1134. /**
  1135. * Moves and resizes this component. The new location of the top-left
  1136. * corner is specified by <code>x</code> and <code>y</code>, and the
  1137. * new size is specified by <code>width</code> and <code>height</code>.
  1138. * @param x The new <i>x</i>-coordinate of this component.
  1139. * @param y The new <i>y</i>-coordinate of this component.
  1140. * @param width The new <code>width</code> of this component.
  1141. * @param height The new <code>height</code> of this
  1142. * component.
  1143. * @see java.awt.Component#getBounds
  1144. * @see java.awt.Component#setLocation(int, int)
  1145. * @see java.awt.Component#setLocation(java.awt.Point)
  1146. * @see java.awt.Component#setSize(int, int)
  1147. * @see java.awt.Component#setSize(java.awt.Dimension)
  1148. * @JDK1.1
  1149. */
  1150. public void setBounds(int x, int y, int width, int height) {
  1151. reshape(x, y, width, height);
  1152. }
  1153. /**
  1154. * @deprecated As of JDK version 1.1,
  1155. * replaced by <code>setBounds(int, int, int, int)</code>.
  1156. */
  1157. public void reshape(int x, int y, int width, int height) {
  1158. synchronized (getTreeLock()) {
  1159. boolean resized = (this.width != width) || (this.height != height);
  1160. boolean moved = (this.x != x) || (this.y != y);
  1161. boolean isLightweight = peer instanceof java.awt.peer.LightweightPeer;
  1162. if (resized) {
  1163. isPacked = false;
  1164. }
  1165. if (resized || moved) {
  1166. if (isLightweight && visible) {
  1167. // Have the parent redraw the area this component occupied.
  1168. repaint();
  1169. }
  1170. this.x = x;
  1171. this.y = y;
  1172. this.width = width;
  1173. this.height = height;
  1174. if (peer != null) {
  1175. if (isLightweight) {
  1176. peer.setBounds(x, y, width, height);
  1177. } else {
  1178. // native peer might be offset by more than direct
  1179. // parent since parent might be lightweight.
  1180. int nativeX = x;
  1181. int nativeY = y;
  1182. for(Component c = parent; (c != null) &&
  1183. (c.peer instanceof java.awt.peer.LightweightPeer);
  1184. c = c.parent) {
  1185. nativeX += c.x;
  1186. nativeY += c.y;
  1187. }
  1188. peer.setBounds(nativeX, nativeY, width, height);
  1189. }
  1190. if (resized) {
  1191. invalidate();
  1192. if (componentListener != null ||
  1193. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0) {
  1194. ComponentEvent e = new ComponentEvent(this,
  1195. ComponentEvent.COMPONENT_RESIZED);
  1196. Toolkit.getEventQueue().postEvent(e);
  1197. }
  1198. }
  1199. if (moved &&
  1200. (componentListener != null ||
  1201. (eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0)) {
  1202. ComponentEvent e = new ComponentEvent(this,
  1203. ComponentEvent.COMPONENT_MOVED);
  1204. Toolkit.getEventQueue().postEvent(e);
  1205. }
  1206. if (parent != null && parent.valid) {
  1207. parent.invalidate();
  1208. }
  1209. }
  1210. if (isLightweight && visible) {
  1211. // Have the parent redraw the area this component *now* occupies.
  1212. repaint();
  1213. }
  1214. }
  1215. }
  1216. }
  1217. /**
  1218. * Moves and resizes this component to conform to the new
  1219. * bounding rectangle <code>r</code>. This component's new
  1220. * position is specified by <code>r.x</code> and <code>r.y</code>,
  1221. * and its new size is specified by <code>r.width</code> and
  1222. * <code>r.height</code>
  1223. * @param r The new bounding rectangle for this component.
  1224. * @see java.awt.Component#getBounds
  1225. * @see java.awt.Component#setLocation(int, int)
  1226. * @see java.awt.Component#setLocation(java.awt.Point)
  1227. * @see java.awt.Component#setSize(int, int)
  1228. * @see java.awt.Component#setSize(java.awt.Dimension)
  1229. * @since JDK1.1
  1230. */
  1231. public void setBounds(Rectangle r) {
  1232. setBounds(r.x, r.y, r.width, r.height);
  1233. }
  1234. /**
  1235. * Return the current x coordinate of the components origin.
  1236. * This method is preferable to writing component.getBounds().x,
  1237. * or component.getLocation().x because it doesn't cause any
  1238. * heap allocations.
  1239. *
  1240. * @return the current x coordinate of the components origin.
  1241. * @since JDK1.2
  1242. */
  1243. public int getX() {
  1244. return x;
  1245. }
  1246. /**
  1247. * Return the current y coordinate of the components origin.
  1248. * This method is preferable to writing component.getBounds().y,
  1249. * or component.getLocation().y because it doesn't cause any
  1250. * heap allocations.
  1251. *
  1252. * @return the current y coordinate of the components origin.
  1253. * @since JDK1.2
  1254. */
  1255. public int getY() {
  1256. return y;
  1257. }
  1258. /**
  1259. * Return the current width of this component.
  1260. * This method is preferable to writing component.getBounds().width,
  1261. * or component.getSize().width because it doesn't cause any
  1262. * heap allocations.
  1263. *
  1264. * @return the current width of this component.
  1265. * @since JDK1.2
  1266. */
  1267. public int getWidth() {
  1268. return width;
  1269. }
  1270. /**
  1271. * Return the current height of this component.
  1272. * This method is preferable to writing component.getBounds().height,
  1273. * or component.getSize().height because it doesn't cause any
  1274. * heap allocations.
  1275. *
  1276. * @return the current height of this component.
  1277. * @since JDK1.2
  1278. */
  1279. public int getHeight() {
  1280. return height;
  1281. }
  1282. /**
  1283. * Store the bounds of this component into "return value" <b>rv</b> and
  1284. * return <b>rv</b>. If rv is null a new Rectangle is allocated.
  1285. * This version of getBounds() is useful if the caller
  1286. * wants to avoid allocating a new Rectangle object on the heap.
  1287. *
  1288. * @param rv the return value, modified to the components bounds
  1289. * @return rv
  1290. */
  1291. public Rectangle getBounds(Rectangle rv) {
  1292. if (rv == null) {
  1293. return new Rectangle(getX(), getY(), getWidth(), getHeight());
  1294. }
  1295. else {
  1296. rv.setBounds(getX(), getY(), getWidth(), getHeight());
  1297. return rv;
  1298. }
  1299. }
  1300. /**
  1301. * Store the width/height of this component into "return value" <b>rv</b>
  1302. * and return <b>rv</b>. If rv is null a new Dimension object is
  1303. * allocated. This version of getSize() is useful if the
  1304. * caller wants to avoid allocating a new Dimension object on the heap.
  1305. *
  1306. * @param rv the return value, modified to the components size
  1307. * @return rv
  1308. */
  1309. public Dimension getSize(Dimension rv) {
  1310. if (rv == null) {
  1311. return new Dimension(getWidth(), getHeight());
  1312. }
  1313. else {
  1314. rv.setSize(getWidth(), getHeight());
  1315. return rv;
  1316. }
  1317. }
  1318. /**
  1319. * Store the x,y origin of this component into "return value" <b>rv</b>
  1320. * and return <b>rv</b>. If rv is null a new Point is allocated.
  1321. * This version of getLocation() is useful if the
  1322. * caller wants to avoid allocating a new Point object on the heap.
  1323. *
  1324. * @param rv the return value, modified to the components location
  1325. * @return rv
  1326. */
  1327. public Point getLocation(Point rv) {
  1328. if (rv == null) {
  1329. return new Point(getX(), getY());
  1330. }
  1331. else {
  1332. rv.setLocation(getX(), getY());
  1333. return rv;
  1334. }
  1335. }
  1336. /**
  1337. * Returns true if this component is completely opaque, returns
  1338. * false by default.
  1339. * <p>
  1340. * An opaque component paints every pixel within its
  1341. * rectangular region. A non-opaque component paints only some of
  1342. * its pixels, allowing the pixels underneath it to "show through".
  1343. * A component that does not fully paint its pixels therefore
  1344. * provides a degree of transparency. Only lightweight
  1345. * components can be transparent.
  1346. * <p>
  1347. * Subclasses that guarantee to always completely paint their
  1348. * contents should override this method and return true. All
  1349. * of the "heavyweight" AWT components are opaque.
  1350. *
  1351. * @return true if this component is completely opaque.
  1352. * @see #isLightweight
  1353. * @since JDK1.2
  1354. */
  1355. public boolean isOpaque() {
  1356. return !isLightweight();
  1357. }
  1358. /**
  1359. * A lightweight component doesn't have a native toolkit peer.
  1360. * Subclasses of Component and Container, other than the ones
  1361. * defined in this package like Button or Scrollbar, are lightweight.
  1362. * All of the Swing components are lightweights.
  1363. *
  1364. * @return true if this component doesn't have a native peer
  1365. * @since JDK1.2
  1366. */
  1367. public boolean isLightweight() {
  1368. return getPeer() instanceof java.awt.peer.LightweightPeer;
  1369. }
  1370. /**
  1371. * Gets the preferred size of this component.
  1372. * @return A dimension object indicating this component's preferred size.
  1373. * @see #getMinimumSize
  1374. * @see java.awt.LayoutManager
  1375. */
  1376. public Dimension getPreferredSize() {
  1377. return preferredSize();
  1378. }
  1379. /**
  1380. * @deprecated As of JDK version 1.1,
  1381. * replaced by <code>getPreferredSize()</code>.
  1382. */
  1383. public Dimension preferredSize() {
  1384. /* Avoid grabbing the lock if a reasonable cached size value
  1385. * is available.
  1386. */
  1387. Dimension dim = prefSize;
  1388. if (dim != null && isValid()) {
  1389. return dim;
  1390. }
  1391. synchronized (getTreeLock()) {
  1392. prefSize = (peer != null) ?
  1393. peer.preferredSize() :
  1394. getMinimumSize();
  1395. return prefSize;
  1396. }
  1397. }
  1398. /**
  1399. * Gets the mininimum size of this component.
  1400. * @return A dimension object indicating this component's minimum size.
  1401. * @see #getPreferredSize
  1402. * @see java.awtLayoutManager
  1403. */
  1404. public Dimension getMinimumSize() {
  1405. return minimumSize();
  1406. }
  1407. /**
  1408. * @deprecated As of JDK version 1.1,
  1409. * replaced by <code>getMinimumSize()</code>.
  1410. */
  1411. public Dimension minimumSize() {
  1412. /* Avoid grabbing the lock if a reasonable cached size value
  1413. * is available.
  1414. */
  1415. Dimension dim = minSize;
  1416. if (dim != null && isValid()) {
  1417. return dim;
  1418. }
  1419. synchronized (getTreeLock()) {
  1420. minSize = (peer != null) ?
  1421. peer.minimumSize() :
  1422. size();
  1423. return minSize;
  1424. }
  1425. }
  1426. /**
  1427. * Gets the maximum size of this component.
  1428. * @return A dimension object indicating this component's maximum size.
  1429. * @see #getMinimumSize
  1430. * @see #getPreferredSize
  1431. * @see LayoutManager
  1432. */
  1433. public Dimension getMaximumSize() {
  1434. return new Dimension(Short.MAX_VALUE, Short.MAX_VALUE);
  1435. }
  1436. /**
  1437. * Returns the alignment along the x axis. This specifies how
  1438. * the component would like to be aligned relative to other
  1439. * components. The value should be a number between 0 and 1
  1440. * where 0 represents alignment along the origin, 1 is aligned
  1441. * the furthest away from the origin, 0.5 is centered, etc.
  1442. */
  1443. public float getAlignmentX() {
  1444. return CENTER_ALIGNMENT;
  1445. }
  1446. /**
  1447. * Returns the alignment along the y axis. This specifies how
  1448. * the component would like to be aligned relative to other
  1449. * components. The value should be a number between 0 and 1
  1450. * where 0 represents alignment along the origin, 1 is aligned
  1451. * the furthest away from the origin, 0.5 is centered, etc.
  1452. */
  1453. public float getAlignmentY() {
  1454. return CENTER_ALIGNMENT;
  1455. }
  1456. /**
  1457. * Prompts the layout manager to lay out this component. This is
  1458. * usually called when the component (more specifically, container)
  1459. * is validated.
  1460. * @see #validate
  1461. * @see LayoutManager
  1462. */
  1463. public void doLayout() {
  1464. layout();
  1465. }
  1466. /**
  1467. * @deprecated As of JDK version 1.1,
  1468. * replaced by <code>doLayout()</code>.
  1469. */
  1470. public void layout() {
  1471. }
  1472. /**
  1473. * Ensures that this component has a valid layout. This method is
  1474. * primarily intended to operate on instances of <code>Container</code>.
  1475. * @see java.awt.Component#invalidate
  1476. * @see java.awt.Component#doLayout()
  1477. * @see java.awt.LayoutManager
  1478. * @see java.awt.Container#validate
  1479. * @since JDK1.0
  1480. */
  1481. public void validate() {
  1482. if (!valid) {
  1483. synchronized (getTreeLock()) {
  1484. ComponentPeer peer = this.peer;
  1485. if (!valid && peer != null) {
  1486. Font newfont = getFont();
  1487. Font oldfont = peerFont;
  1488. if (newfont != oldfont && (oldfont == null
  1489. || !oldfont.equals(newfont))) {
  1490. peer.setFont(newfont);
  1491. peerFont = newfont;
  1492. }
  1493. }
  1494. }
  1495. valid = true;
  1496. }
  1497. }
  1498. /**
  1499. * Invalidates this component. This component and all parents
  1500. * above it are marked as needing to be laid out. This method can
  1501. * be called often, so it needs to execute quickly.
  1502. * @see java.awt.Component#validate
  1503. * @see java.awt.Component#doLayout
  1504. * @see java.awt.LayoutManager
  1505. * @since JDK1.0
  1506. */
  1507. public void invalidate() {
  1508. synchronized (getTreeLock()) {
  1509. /* Nullify cached layout and size information.
  1510. * For efficiency, propagate invalidate() upwards only if
  1511. * some other component hasn't already done so first.
  1512. */