jdk/src/share/classes/java/awt/Component.java

Print this page




1733                     Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
1734                     ComponentEvent e = new ComponentEvent(this,
1735                                                           ComponentEvent.COMPONENT_HIDDEN);
1736                     Toolkit.getEventQueue().postEvent(e);
1737                 }
1738             }
1739             Container parent = this.parent;
1740             if (parent != null) {
1741                 parent.invalidate();
1742             }
1743         }
1744     }
1745 
1746     /**
1747      * Gets the foreground color of this component.
1748      * @return this component's foreground color; if this component does
1749      * not have a foreground color, the foreground color of its parent
1750      * is returned
1751      * @see #setForeground
1752      * @since 1.0
1753      * @beaninfo
1754      *       bound: true
1755      */
1756     @Transient
1757     public Color getForeground() {
1758         Color foreground = this.foreground;
1759         if (foreground != null) {
1760             return foreground;
1761         }
1762         Container parent = this.parent;
1763         return (parent != null) ? parent.getForeground() : null;
1764     }
1765 
1766     /**
1767      * Sets the foreground color of this component.
1768      * @param c the color to become this component's
1769      *          foreground color; if this parameter is <code>null</code>
1770      *          then this component will inherit
1771      *          the foreground color of its parent
1772      * @see #getForeground
1773      * @since 1.0
1774      */


1813         Color background = this.background;
1814         if (background != null) {
1815             return background;
1816         }
1817         Container parent = this.parent;
1818         return (parent != null) ? parent.getBackground() : null;
1819     }
1820 
1821     /**
1822      * Sets the background color of this component.
1823      * <p>
1824      * The background color affects each component differently and the
1825      * parts of the component that are affected by the background color
1826      * may differ between operating systems.
1827      *
1828      * @param c the color to become this component's color;
1829      *          if this parameter is <code>null</code>, then this
1830      *          component will inherit the background color of its parent
1831      * @see #getBackground
1832      * @since 1.0
1833      * @beaninfo
1834      *       bound: true
1835      */
1836     public void setBackground(Color c) {
1837         Color oldColor = background;
1838         ComponentPeer peer = this.peer;
1839         background = c;
1840         if (peer != null) {
1841             c = getBackground();
1842             if (c != null) {
1843                 peer.setBackground(c);
1844             }
1845         }
1846         // This is a bound property, so report the change to
1847         // any registered listeners.  (Cheap if there are none.)
1848         firePropertyChange("background", oldColor, c);
1849     }
1850 
1851     /**
1852      * Returns whether the background color has been explicitly set for this
1853      * Component. If this method returns <code>false</code>, this Component is
1854      * inheriting its background color from an ancestor.


1881         Font font = this.font;
1882         if (font != null) {
1883             return font;
1884         }
1885         Container parent = this.parent;
1886         return (parent != null) ? parent.getFont_NoClientCode() : null;
1887     }
1888 
1889     /**
1890      * Sets the font of this component.
1891      * <p>
1892      * This method changes layout-related information, and therefore,
1893      * invalidates the component hierarchy.
1894      *
1895      * @param f the font to become this component's font;
1896      *          if this parameter is <code>null</code> then this
1897      *          component will inherit the font of its parent
1898      * @see #getFont
1899      * @see #invalidate
1900      * @since 1.0
1901      * @beaninfo
1902      *       bound: true
1903      */
1904     public void setFont(Font f) {
1905         Font oldFont, newFont;
1906         synchronized(getTreeLock()) {
1907             oldFont = font;
1908             newFont = font = f;
1909             ComponentPeer peer = this.peer;
1910             if (peer != null) {
1911                 f = getFont();
1912                 if (f != null) {
1913                     peer.setFont(f);
1914                     peerFont = f;
1915                 }
1916             }
1917         }
1918         // This is a bound property, so report the change to
1919         // any registered listeners.  (Cheap if there are none.)
1920         firePropertyChange("font", oldFont, newFont);
1921 
1922         // This could change the preferred size of the Component.


7199 
7200     /**
7201      * Returns whether this Component can be focused.
7202      *
7203      * @return <code>true</code> if this Component is focusable;
7204      *         <code>false</code> otherwise.
7205      * @see #setFocusable
7206      * @since 1.4
7207      */
7208     public boolean isFocusable() {
7209         return isFocusTraversable();
7210     }
7211 
7212     /**
7213      * Sets the focusable state of this Component to the specified value. This
7214      * value overrides the Component's default focusability.
7215      *
7216      * @param focusable indicates whether this Component is focusable
7217      * @see #isFocusable
7218      * @since 1.4
7219      * @beaninfo
7220      *       bound: true
7221      */
7222     public void setFocusable(boolean focusable) {
7223         boolean oldFocusable;
7224         synchronized (this) {
7225             oldFocusable = this.focusable;
7226             this.focusable = focusable;
7227         }
7228         isFocusTraversableOverridden = FOCUS_TRAVERSABLE_SET;
7229 
7230         firePropertyChange("focusable", oldFocusable, focusable);
7231         if (oldFocusable && !focusable) {
7232             if (isFocusOwner() && KeyboardFocusManager.isAutoFocusTransferEnabled()) {
7233                 transferFocus(true);
7234             }
7235             KeyboardFocusManager.clearMostRecentFocusOwner(this);
7236         }
7237     }
7238 
7239     final boolean isFocusTraversableOverridden() {
7240         return (isFocusTraversableOverridden != FOCUS_TRAVERSABLE_DEFAULT);


7292      * <p>
7293      * This method may throw a {@code ClassCastException} if any {@code Object}
7294      * in {@code keystrokes} is not an {@code AWTKeyStroke}.
7295      *
7296      * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
7297      *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
7298      *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
7299      * @param keystrokes the Set of AWTKeyStroke for the specified operation
7300      * @see #getFocusTraversalKeys
7301      * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
7302      * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
7303      * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
7304      * @throws IllegalArgumentException if id is not one of
7305      *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
7306      *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
7307      *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or if keystrokes
7308      *         contains null, or if any keystroke represents a KEY_TYPED event,
7309      *         or if any keystroke already maps to another focus traversal
7310      *         operation for this Component
7311      * @since 1.4
7312      * @beaninfo
7313      *       bound: true
7314      */
7315     public void setFocusTraversalKeys(int id,
7316                                       Set<? extends AWTKeyStroke> keystrokes)
7317     {
7318         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
7319             throw new IllegalArgumentException("invalid focus traversal key identifier");
7320         }
7321 
7322         setFocusTraversalKeys_NoIDCheck(id, keystrokes);
7323     }
7324 
7325     /**
7326      * Returns the Set of focus traversal keys for a given traversal operation
7327      * for this Component. (See
7328      * <code>setFocusTraversalKeys</code> for a full description of each key.)
7329      * <p>
7330      * If a Set of traversal keys has not been explicitly defined for this
7331      * Component, then this Component's parent's Set is returned. If no Set
7332      * has been explicitly defined for any of this Component's ancestors, then
7333      * the current KeyboardFocusManager's default Set is returned.


7444         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
7445             throw new IllegalArgumentException("invalid focus traversal key identifier");
7446         }
7447 
7448         return (focusTraversalKeys != null && focusTraversalKeys[id] != null);
7449     }
7450 
7451     /**
7452      * Sets whether focus traversal keys are enabled for this Component.
7453      * Components for which focus traversal keys are disabled receive key
7454      * events for focus traversal keys. Components for which focus traversal
7455      * keys are enabled do not see these events; instead, the events are
7456      * automatically converted to traversal operations.
7457      *
7458      * @param focusTraversalKeysEnabled whether focus traversal keys are
7459      *        enabled for this Component
7460      * @see #getFocusTraversalKeysEnabled
7461      * @see #setFocusTraversalKeys
7462      * @see #getFocusTraversalKeys
7463      * @since 1.4
7464      * @beaninfo
7465      *       bound: true
7466      */
7467     public void setFocusTraversalKeysEnabled(boolean
7468                                              focusTraversalKeysEnabled) {
7469         boolean oldFocusTraversalKeysEnabled;
7470         synchronized (this) {
7471             oldFocusTraversalKeysEnabled = this.focusTraversalKeysEnabled;
7472             this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
7473         }
7474         firePropertyChange("focusTraversalKeysEnabled",
7475                            oldFocusTraversalKeysEnabled,
7476                            focusTraversalKeysEnabled);
7477     }
7478 
7479     /**
7480      * Returns whether focus traversal keys are enabled for this Component.
7481      * Components for which focus traversal keys are disabled receive key
7482      * events for focus traversal keys. Components for which focus traversal
7483      * keys are enabled do not see these events; instead, the events are
7484      * automatically converted to traversal operations.
7485      *


8960      * At construction time, a component's orientation is set to
8961      * <code>ComponentOrientation.UNKNOWN</code>,
8962      * indicating that it has not been specified
8963      * explicitly.  The UNKNOWN orientation behaves the same as
8964      * <code>ComponentOrientation.LEFT_TO_RIGHT</code>.
8965      * <p>
8966      * To set the orientation of a single component, use this method.
8967      * To set the orientation of an entire component
8968      * hierarchy, use
8969      * {@link #applyComponentOrientation applyComponentOrientation}.
8970      * <p>
8971      * This method changes layout-related information, and therefore,
8972      * invalidates the component hierarchy.
8973      *
8974      * @param  o the orientation to be set
8975      *
8976      * @see ComponentOrientation
8977      * @see #invalidate
8978      *
8979      * @author Laura Werner, IBM
8980      * @beaninfo
8981      *       bound: true
8982      */
8983     public void setComponentOrientation(ComponentOrientation o) {
8984         ComponentOrientation oldValue = componentOrientation;
8985         componentOrientation = o;
8986 
8987         // This is a bound property, so report the change to
8988         // any registered listeners.  (Cheap if there are none.)
8989         firePropertyChange("componentOrientation", oldValue, o);
8990 
8991         // This could change the preferred size of the Component.
8992         invalidateIfValid();
8993     }
8994 
8995     /**
8996      * Retrieves the language-sensitive orientation that is to be used to order
8997      * the elements or text within this component.  <code>LayoutManager</code>
8998      * and <code>Component</code>
8999      * subclasses that wish to respect orientation should call this method to
9000      * get the component's orientation before performing layout or drawing.
9001      *




1733                     Toolkit.enabledOnToolkit(AWTEvent.COMPONENT_EVENT_MASK)) {
1734                     ComponentEvent e = new ComponentEvent(this,
1735                                                           ComponentEvent.COMPONENT_HIDDEN);
1736                     Toolkit.getEventQueue().postEvent(e);
1737                 }
1738             }
1739             Container parent = this.parent;
1740             if (parent != null) {
1741                 parent.invalidate();
1742             }
1743         }
1744     }
1745 
1746     /**
1747      * Gets the foreground color of this component.
1748      * @return this component's foreground color; if this component does
1749      * not have a foreground color, the foreground color of its parent
1750      * is returned
1751      * @see #setForeground
1752      * @since 1.0


1753      */
1754     @Transient
1755     public Color getForeground() {
1756         Color foreground = this.foreground;
1757         if (foreground != null) {
1758             return foreground;
1759         }
1760         Container parent = this.parent;
1761         return (parent != null) ? parent.getForeground() : null;
1762     }
1763 
1764     /**
1765      * Sets the foreground color of this component.
1766      * @param c the color to become this component's
1767      *          foreground color; if this parameter is <code>null</code>
1768      *          then this component will inherit
1769      *          the foreground color of its parent
1770      * @see #getForeground
1771      * @since 1.0
1772      */


1811         Color background = this.background;
1812         if (background != null) {
1813             return background;
1814         }
1815         Container parent = this.parent;
1816         return (parent != null) ? parent.getBackground() : null;
1817     }
1818 
1819     /**
1820      * Sets the background color of this component.
1821      * <p>
1822      * The background color affects each component differently and the
1823      * parts of the component that are affected by the background color
1824      * may differ between operating systems.
1825      *
1826      * @param c the color to become this component's color;
1827      *          if this parameter is <code>null</code>, then this
1828      *          component will inherit the background color of its parent
1829      * @see #getBackground
1830      * @since 1.0


1831      */
1832     public void setBackground(Color c) {
1833         Color oldColor = background;
1834         ComponentPeer peer = this.peer;
1835         background = c;
1836         if (peer != null) {
1837             c = getBackground();
1838             if (c != null) {
1839                 peer.setBackground(c);
1840             }
1841         }
1842         // This is a bound property, so report the change to
1843         // any registered listeners.  (Cheap if there are none.)
1844         firePropertyChange("background", oldColor, c);
1845     }
1846 
1847     /**
1848      * Returns whether the background color has been explicitly set for this
1849      * Component. If this method returns <code>false</code>, this Component is
1850      * inheriting its background color from an ancestor.


1877         Font font = this.font;
1878         if (font != null) {
1879             return font;
1880         }
1881         Container parent = this.parent;
1882         return (parent != null) ? parent.getFont_NoClientCode() : null;
1883     }
1884 
1885     /**
1886      * Sets the font of this component.
1887      * <p>
1888      * This method changes layout-related information, and therefore,
1889      * invalidates the component hierarchy.
1890      *
1891      * @param f the font to become this component's font;
1892      *          if this parameter is <code>null</code> then this
1893      *          component will inherit the font of its parent
1894      * @see #getFont
1895      * @see #invalidate
1896      * @since 1.0


1897      */
1898     public void setFont(Font f) {
1899         Font oldFont, newFont;
1900         synchronized(getTreeLock()) {
1901             oldFont = font;
1902             newFont = font = f;
1903             ComponentPeer peer = this.peer;
1904             if (peer != null) {
1905                 f = getFont();
1906                 if (f != null) {
1907                     peer.setFont(f);
1908                     peerFont = f;
1909                 }
1910             }
1911         }
1912         // This is a bound property, so report the change to
1913         // any registered listeners.  (Cheap if there are none.)
1914         firePropertyChange("font", oldFont, newFont);
1915 
1916         // This could change the preferred size of the Component.


7193 
7194     /**
7195      * Returns whether this Component can be focused.
7196      *
7197      * @return <code>true</code> if this Component is focusable;
7198      *         <code>false</code> otherwise.
7199      * @see #setFocusable
7200      * @since 1.4
7201      */
7202     public boolean isFocusable() {
7203         return isFocusTraversable();
7204     }
7205 
7206     /**
7207      * Sets the focusable state of this Component to the specified value. This
7208      * value overrides the Component's default focusability.
7209      *
7210      * @param focusable indicates whether this Component is focusable
7211      * @see #isFocusable
7212      * @since 1.4


7213      */
7214     public void setFocusable(boolean focusable) {
7215         boolean oldFocusable;
7216         synchronized (this) {
7217             oldFocusable = this.focusable;
7218             this.focusable = focusable;
7219         }
7220         isFocusTraversableOverridden = FOCUS_TRAVERSABLE_SET;
7221 
7222         firePropertyChange("focusable", oldFocusable, focusable);
7223         if (oldFocusable && !focusable) {
7224             if (isFocusOwner() && KeyboardFocusManager.isAutoFocusTransferEnabled()) {
7225                 transferFocus(true);
7226             }
7227             KeyboardFocusManager.clearMostRecentFocusOwner(this);
7228         }
7229     }
7230 
7231     final boolean isFocusTraversableOverridden() {
7232         return (isFocusTraversableOverridden != FOCUS_TRAVERSABLE_DEFAULT);


7284      * <p>
7285      * This method may throw a {@code ClassCastException} if any {@code Object}
7286      * in {@code keystrokes} is not an {@code AWTKeyStroke}.
7287      *
7288      * @param id one of KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
7289      *        KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
7290      *        KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS
7291      * @param keystrokes the Set of AWTKeyStroke for the specified operation
7292      * @see #getFocusTraversalKeys
7293      * @see KeyboardFocusManager#FORWARD_TRAVERSAL_KEYS
7294      * @see KeyboardFocusManager#BACKWARD_TRAVERSAL_KEYS
7295      * @see KeyboardFocusManager#UP_CYCLE_TRAVERSAL_KEYS
7296      * @throws IllegalArgumentException if id is not one of
7297      *         KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
7298      *         KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, or
7299      *         KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, or if keystrokes
7300      *         contains null, or if any keystroke represents a KEY_TYPED event,
7301      *         or if any keystroke already maps to another focus traversal
7302      *         operation for this Component
7303      * @since 1.4


7304      */
7305     public void setFocusTraversalKeys(int id,
7306                                       Set<? extends AWTKeyStroke> keystrokes)
7307     {
7308         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
7309             throw new IllegalArgumentException("invalid focus traversal key identifier");
7310         }
7311 
7312         setFocusTraversalKeys_NoIDCheck(id, keystrokes);
7313     }
7314 
7315     /**
7316      * Returns the Set of focus traversal keys for a given traversal operation
7317      * for this Component. (See
7318      * <code>setFocusTraversalKeys</code> for a full description of each key.)
7319      * <p>
7320      * If a Set of traversal keys has not been explicitly defined for this
7321      * Component, then this Component's parent's Set is returned. If no Set
7322      * has been explicitly defined for any of this Component's ancestors, then
7323      * the current KeyboardFocusManager's default Set is returned.


7434         if (id < 0 || id >= KeyboardFocusManager.TRAVERSAL_KEY_LENGTH - 1) {
7435             throw new IllegalArgumentException("invalid focus traversal key identifier");
7436         }
7437 
7438         return (focusTraversalKeys != null && focusTraversalKeys[id] != null);
7439     }
7440 
7441     /**
7442      * Sets whether focus traversal keys are enabled for this Component.
7443      * Components for which focus traversal keys are disabled receive key
7444      * events for focus traversal keys. Components for which focus traversal
7445      * keys are enabled do not see these events; instead, the events are
7446      * automatically converted to traversal operations.
7447      *
7448      * @param focusTraversalKeysEnabled whether focus traversal keys are
7449      *        enabled for this Component
7450      * @see #getFocusTraversalKeysEnabled
7451      * @see #setFocusTraversalKeys
7452      * @see #getFocusTraversalKeys
7453      * @since 1.4


7454      */
7455     public void setFocusTraversalKeysEnabled(boolean
7456                                              focusTraversalKeysEnabled) {
7457         boolean oldFocusTraversalKeysEnabled;
7458         synchronized (this) {
7459             oldFocusTraversalKeysEnabled = this.focusTraversalKeysEnabled;
7460             this.focusTraversalKeysEnabled = focusTraversalKeysEnabled;
7461         }
7462         firePropertyChange("focusTraversalKeysEnabled",
7463                            oldFocusTraversalKeysEnabled,
7464                            focusTraversalKeysEnabled);
7465     }
7466 
7467     /**
7468      * Returns whether focus traversal keys are enabled for this Component.
7469      * Components for which focus traversal keys are disabled receive key
7470      * events for focus traversal keys. Components for which focus traversal
7471      * keys are enabled do not see these events; instead, the events are
7472      * automatically converted to traversal operations.
7473      *


8948      * At construction time, a component's orientation is set to
8949      * <code>ComponentOrientation.UNKNOWN</code>,
8950      * indicating that it has not been specified
8951      * explicitly.  The UNKNOWN orientation behaves the same as
8952      * <code>ComponentOrientation.LEFT_TO_RIGHT</code>.
8953      * <p>
8954      * To set the orientation of a single component, use this method.
8955      * To set the orientation of an entire component
8956      * hierarchy, use
8957      * {@link #applyComponentOrientation applyComponentOrientation}.
8958      * <p>
8959      * This method changes layout-related information, and therefore,
8960      * invalidates the component hierarchy.
8961      *
8962      * @param  o the orientation to be set
8963      *
8964      * @see ComponentOrientation
8965      * @see #invalidate
8966      *
8967      * @author Laura Werner, IBM


8968      */
8969     public void setComponentOrientation(ComponentOrientation o) {
8970         ComponentOrientation oldValue = componentOrientation;
8971         componentOrientation = o;
8972 
8973         // This is a bound property, so report the change to
8974         // any registered listeners.  (Cheap if there are none.)
8975         firePropertyChange("componentOrientation", oldValue, o);
8976 
8977         // This could change the preferred size of the Component.
8978         invalidateIfValid();
8979     }
8980 
8981     /**
8982      * Retrieves the language-sensitive orientation that is to be used to order
8983      * the elements or text within this component.  <code>LayoutManager</code>
8984      * and <code>Component</code>
8985      * subclasses that wish to respect orientation should call this method to
8986      * get the component's orientation before performing layout or drawing.
8987      *