Java AWT: Desktop Colors


Last Updated: November 21, 1996

Purpose

It is common for platform desktops (Windows95, Solaris/CDE, etc.) to provide a color scheme for objects on the desktop, and typically this scheme is configurable by the user. It is usually desirable to have applications running on that desktop use that color scheme in order to maintain visual consistency.

The AWT attempts to set appropriate default colors for its components, but there is currently no convenient way for a Java program to query and use those colors directly. This is particularly a problem when creating custom components where you'd like to ensure that the background, shadow, and font colors all match the rest of the desktop. In JDK1.1, the AWT will provide a simple API for accessing and using these desktop colors.

Desktop Color API

The API involves introducing a new type of "symbolic" color, which is represented by a subclass of java.awt.Color:

        java.awt.SystemColor
A SystemColor object can be used just like any other Color object, the only difference being that the actual value that represents its current color may change dynamically (on systems which support dynamic notification when the user changes the color scheme).

SystemColor objects are defined by the system and cannot be instantiated by Java programs.

Range of Colors

The SystemColor class provides a standard set of these symbolic colors that represent each distinct type of coloration on the desktop. These symbolic color objects are created automatically and stored statically in java.awt.SystemColor:
	public final static Color desktop; // Background color of desktop
	public final static Color activeCaption; // Background color for captions
	public final static Color activeCaptionText; // Text color for captions
	public final static Color activeCaptionBorder; // Border color for caption text
	public final static Color inactiveCaption; // Background color for inactive captions
	public final static Color inactiveCaptionText; // Text color for inactive captions
	public final static Color inactiveCaptionBorder; // Border color for inactive captions
	public final static Color window; // Background for windows
	public final static Color windowBorder; // Color of window border frame
	public final static Color windowText; // Text color inside windows
	public final static Color menu; // Background for menus
	public final static Color menuText; // Text color for menus
	public final static Color text; // background color for text
	public final static Color textText; // text color for text
	public final static Color textHighlight; // background color for highlighted text
	public final static Color textHighlightText; // text color for highlighted text
	public final static Color control; // Background color for controls
	public final static Color controlText; // Text color for controls
	public final static Color controlLtHighlight; // Light highlight color for controls
	public final static Color controlHighlight; // Highlight color for controls
	public final static Color controlShadow; // Shadow color for controls
	public final static Color controlDkShadow; // Dark shadow color for controls
	public final static Color inactiveControlText; // Text color for inactive controls
	public final static Color scrollbar; // Background color for scrollbars
	public final static Color info; // Background color for spot-help text
	public final static Color infoText; // Text color for spot-help text

If a Java program either sets the foreground or background of a component to be one of these symbolic colors, or uses one of these for rendering, the current value for that symbolic color will be used.

Not all platforms support distinct colors for each of these symbolic names. In the cases where a symbolic color is not supported by a platform, it is mapped to the most appropriate default. These colors are guaranteed to be non-null.

The AWT base components will be modified to use these symbolic colors in order to provide more consistent default integration with the desktop. Java programs written for 1.0 which make any assumptions about the default color of components (i.e. assume they will be "gray") may not render correctly under 1.1. It is always important to query the component for its background/foreground colors within paint() methods for this reason.

One-time vs. Dynamic Updates

At a minimum, these colors are initialized dynamically when the toolkit is first loaded. Some desktops provide dynamic update capabilities such that the AWT can detect when the user changes the color scheme and then update all these symbolic colors on the fly. However, this is platform-specific and is not a guaranteed feature of this interface.

Sample Code

Following is an example of a custom component (a separator) which uses this API:
	class Separator extends Canvas {
	    public Separator(int length, int thickness) {
		super();
		resize(length, thickness);
		setBackground(SystemColor.control);
	    }
	    public void paint(Graphics g) {
		int x1, y1, x2, y2;
		Rectangle bbox = bounds();
	
		x1 = 0;
		x2 = bbox.width - 1;
		y1 = y2 = bbox.height/2 - 1;
	
		g.setColor(SystemColor.controlShadow);
		g.drawLine(x1, y2, x2, y2);
		g.setColor(SystemColor.controlHighlight);
		g.drawLine(x1, y2+1, x2, y2+1);
	    }
	}
	
		


Send feedback to:java-awt@java.sun.com
Copyright © 1996, Sun Microsystems, Inc. All rights reserved.