[Chapter 3] 3.4 SystemColor

#

| Chapter 3 Fonts and Colors |

## 3.4 SystemColor

In Java 1.1, AWT provides access to desktop color schemes, or themes. To give you an idea of how these themes work, with the Windows Standard scheme for the Windows 95 desktop, buttons have a gray background with black text. If you use the control panel to change to a High Contrast Black scheme, the button's background becomes black and the text white. Prior to 1.1, Java didn't know anything about desktop colors: all color values were hard coded. If you asked for a particular shade of gray, you got that shade, and that was it; applets and applications had no knowledge of the desktop color scheme in effect, and therefore, wouldn't change in response to changes in the color scheme.

Starting with Java 1.1, you can write programs that react to changes in the color scheme: for example, a button's color will change automatically when you use the control panel to change the color scheme. To do so, you use a large number of constants that are defined in the `SystemColor` class. Although these constants are `public` `static` `final`, they actually have a very strange behavior. Your program is not allowed to modify them (like any other constant). However, their initial values are loaded at run-time, and their values may change, corresponding to changes in the color scheme. This has one important consequence for programmers: you should not use `equals()`to compare a `SystemColor` with a "regular" `Color`; use the `getRGB()` methods of the colors you are comparing to ensure that you compare the current color value.[1] Using Desktop Colors contains a usage example.

> [1] The omission of an `equals()` method that can properly compare a `SystemColor` with a `Color` is unfortunate.

Because `SystemColor` is a subclass of `Color`, you can use a `SystemColor` anywhere you can use a `Color` object. You will never create your own `SystemColor` objects; there is no public constructor. The only objects in this class are the twenty or so `SystemColor` constants.

### SystemColor Methods

Constants

There are two sets of constants within `SystemColor`. The first set provides names for indices into the internal system color lookup table; you will probably never need to use these. All of them have corresponding constants in the second set, except `SystemColor.NUM_COLORS`, which tells you how many `SystemColor` constants are in the second set.

public final static int ACTIVE_CAPTION
public final static int ACTIVE_CAPTION_BORDER
public final static int ACTIVE_CAPTION_TEXT
public final static int CONTROL
public final static int CONTROL_DK_SHADOW
public final static int CONTROL_HIGHLIGHT
public final static int CONTROL_LT_HIGHLIGHT
public final static int CONTROL_SHADOW
public final static int CONTROL_TEXT
public final static int DESKTOP
public final static int INACTIVE_CAPTION
public final static int INACTIVE_CAPTION_BORDER
public final static int INACTIVE_CAPTION_TEXT
public final static int INFO
public final static int INFO_TEXT
public final static int MENU
public final static int MENU_TEXT
public final static int NUM_COLORS
public final static int SCROLLBAR
public final static int TEXT
public final static int TEXT_HIGHLIGHT
public final static int TEXT_HIGHLIGHT_TEXT
public final static int TEXT_INACTIVE_TEXT
public final static int TEXT_TEXT
public final static int WINDOW
public final static int WINDOW_BORDER
public final static int WINDOW_TEXT

The second set of constants is the set of `SystemColor`s you use when creating `Component` objects, to ensure they appear similar to other objects in the user's desktop environment. By using these symbolic constants, you can create new objects that are well integrated into the user's desktop environment, making it easier for the user to work with your program.

public final static SystemColor activeCaption

  The `activeCaption` color represents the background color for the active window's title area. This is automatically set for you when you use `Frame`.

public final static SystemColor activeCaptionBorder

  The `activeCaptionBorder` color represents the border color for the active window.

public final static SystemColor activeCaptionText

  The `activeCaptionText` color represents the text color to use for the active window's title.

public final static SystemColor control

  The `control` color represents the background color for the different components. If you are creating your own `Component` by subclassing `Canvas`, this should be the background color of the new object.

public final static SystemColor controlDkShadow

  The `controlDkShadow` color represents a dark shadow color to be used with `control` and `controlShadow` to simulate a three-dimensional appearance. Ordinarily, when not depressed, the `controlDkShadow` should be used for the object's bottom and right edges. When depressed, `controlDkShadow` should be used for the top and left edges.

public final static SystemColor controlHighlight

  The `controlHighlight` color represents an emphasis color for use in an area or an item of a custom component.

public final static SystemColor controlLtHighlight

  The `controlLtHighlight` color represents a lighter emphasis color for use in an area or an item of a custom component.

public final static SystemColor controlShadow

  The `controlShadow` color represents a light shadow color to be used with `control` and `controlDkShadow` to simulate a three-dimensional appearance. Ordinarily, when not depressed, the `controlShadow` should be used for the top and left edges. When depressed, `controlShadow` should be used for the bottom and right edges.

public final static SystemColor controlText

  The `controlText` color represents the text color of a component. Before drawing any text in your own components, you should change the color to `controlText` with a statement like this:

    g.setColor(SystemColor.controlText);

public final static SystemColor desktop

  The `desktop` color represents the background color of the desktop workspace.

public final static SystemColor inactiveCaption

  The `inactiveCaption` color represents the background color for an inactive window's title area.

public final static SystemColor inactiveCaptionBorder

  The `inactiveCaptionBorder` color represents the border color for an inactive window.

public final static SystemColor inactiveCaptionText

  The `inactiveCaptionText` color represents the text color to use for each inactive window's title.

public final static SystemColor info

  The `info` color represents the background color for mouse-over help text. When a mouse dwells over an object, any pop-up help text should be displayed in an area of this color. In the Microsoft Windows world, these are also called "tool tips."

public final static SystemColor infoText

  The `infoText` color represents the text color for mouse-over help text.

public final static SystemColor menu

  The `menu` color represents the background color of deselected `MenuItem`-like objects. When the menu is selected, the `textHighlight` color is normally the background color.

public final static SystemColor menuText

  The `menuText` color represents the color of the text on deselected `MenuItem`-like objects. When a menu is selected, the `textHighlightText` color is normally the text color. If the menu happens to be inactive, `textInactiveText` would be used.

public final static SystemColor scrollbar

  The `scrollbar` color represents the background color for scrollbars. This color is used by default with `Scrollbar`, `ScrollPane`, `TextArea`, and `List` objects.

public final static SystemColor textHighlight

  The `textHighlight` color represents the background color of highlighted text; for example, it is used for the selected area of a `TextField` or a selected `MenuItem`.

public final static SystemColor textHighlightText

  The `textHighlightText` color represents the text color of highlighted text.

public final static SystemColor textInactiveText

  The `textInactiveText` color represents the text color of an inactive component.

public final static SystemColor textText

  The `textText` color represents the color of text in `TextComponent` objects.

public final static SystemColor window

  The `window` color represents the background color of the window's display area. For an applet, this would be the display area specified by the `WIDTH` and `HEIGHT` values of the `<APPLET>` tag (`setBackground(SystemColor.window)`), although you would probably use it more for the background of a `Frame`.

public final static SystemColor windowBorder

  The `windowBorder` color represents the color of the borders around a window. With AWT, instances of `Window` do not have borders, but instances of `Frame` and `Dialog` do.

public final static SystemColor windowText

  The `windowText` color represents the color of the text drawn within the window.

> NOTE:

Every platform does not fully support every system color. However, on platforms that do not provide natural values for some constants, Java selects reasonable alternate colors.

If you are going to be working only with Java's prefabricated components (`Button`, `List`, etc.), you don't have to worry about system colors; the component's default colors will be set appropriately. You are most likely to use system colors if you are creating your own components. In this case, you will use system colors to make your component emulate the behavior of other components; for example, you will use `controlText` as the color for drawing text, `activeCaption` as the background for the caption of an active window, and so on. Constructors

There are no public constructors for `SystemColor`. If you need to create a new color, use the `Color` class described previously. Miscellaneous methods

public int getRGB ()

  The `getRGB()` method retrieves the current settings for red, green, and blue in one combined value, like `Color`. However, since the color value is dynamic, `getRGB()` needs to look up the value in an internal table. Therefore, `SystemColor` overrides `Color.getRGB()`.

public String toString ()

  The `toString()` method of `SystemColor` returns a string showing the system color's index into its internal table. For example, the following string is returned by `SystemColor.text.toString()`:

    java.awt.SystemColor[i=12]

Color |  | Displaying Colors