Java tip: SystemColors for Linux Gnome user interface themes

Technologies: Java 5+

Linux's Gnome defines themes that set the color scheme and style for window frames, buttons, scrollbars, and other components drawn by Gnome's GTK user interface toolkit. For Java applications, changing the theme should set colors in Java's java.awt.SystemColor objects, which applications may use to customize their user interfaces to match the current theme. Unfortunately, there are some problems here.

This article discusses SystemColor problems with Gnome, and provides color swatches, RGB/HSV values, and downloadable color lists for the default SystemColors on Linux.

This article is part of a series on How to use SystemColors to access OS user interface theme colors that catalogs Java's SystemColors for user interface themes in major OSes. The color catalogs provide a resource for comparing theme colors and creating custom Java components that match the themes.

Accessing Gnome theme colors from Java

A Gnome theme defines the color scheme and style for window frames, buttons, and the like. Each Linux distribution comes with a starter set of themes and sometimes a branded theme for the distribution, such as the orange Ambiance theme for Ubuntu, or the light blue Fedora theme for Fedora. Additional themes can be downloaded from Gnome.org, including many that mimic the appearance of Mac, Windows, and KDE themes.

Java's java.awt.SystemColor class provides access to well-defined theme colors, such as the foreground and background colors for buttons, text fields, and menus. Each theme color has a static SystemColor object. For instance, SystemColor.textHighlight is the background color for selected text in a text field.

A list of SystemColor objects and their use is discussed further in the lead series article. The following article discusses Gnome-specific issues and provides color swatches and RGB values for the default SystemColors.

Selecting Gnome themes

Briefly, Gnome themes are selected using the Theme tab of the Appearance Preferences pane. Each theme combines settings for the color scheme, button style, window frame style, and (optionally) the fonts used. The theme used for these screenshots is Ubuntu's default Ambiance theme.

Linux Gnome Appearance Preferences theme tab
Figure 1. Theme selections in the
Gnome Theme tab of the Appearance Preferences pane.

For any theme, clicking on the Customize button on the preference pane shows the Customize Theme control panel with tabs to adjust the button style, color scheme, window style, icon set, and mouse pointers:

Controls tab of the Customize theme control panel Colors tab of the Customize Theme control panel Window Border tab of the Customize Theme control panel
Figure 2. Controls, Colors, and Window Borders in the
Linux Gnome Customize Theme control panel.

Dealing with Gnome SystemColor problems

Java's SystemColor class is a flawed approach to accessing OS theme settings. While colors are defined for button backgrounds and text, none are defined for tabs, checkboxes, radio buttons, progress bars, and more. Though a highlight color is defined for selected text, none are defined for selected menu items, toggle buttons, radio buttons, or checkboxes. Beyond colors, Java has no access to OS theme fonts, button styles, window title bar buttons, and so forth. So, while there are specific problems on Linux (and every OS), the primary problem is that Java's theme access is flawed.

Java's user interface class libraries (java.awt and javax.swing) are implemented by Linux Java distributions such as Oracle's JDK, Oracle's JRocket, IBM's JDK, and the open source OpenJDK and Apache Harmony. On Linux, all of these are built atop the GTK+ user interface toolkit used by Gnome itself.

However... none of these Java distributions initialize the SystemColors to match the Gnome theme. The SystemColor feature is simply broken on Gnome. This is a known bug, but it has gone unfixed for years.

Instead, on Linux the SystemColors are initialized to generic defaults that have nothing to do with the current Gnome theme, and they don't change when the Gnome theme is changed. Worse, the generic defaults depend upon the Java distribution in use. Oracle's JDK, Oracle's JRocket, IBM's JDK, and the OpenJDK all initialize the colors one way. Apache's Harmony initializes them another way.

Fortunately, there is a work-around. On Linux, the UIDefaults for the native "GTK+" look and feel are set to the proper Gnome theme values, and they update as the theme is changed. These look and feel defaults can be accessed whether or not the Java application uses the look and feel.

Color swatches and values

While the bugs in the SystemColor implementation insure that the class has almost no use on Linux, the color swatches below nevertheless show the initialized (wrong) defaults for several Java distributions.

Oracle JDK, Oracle JRocket, OpenJDK, and IBM JDK

Oracle's JDK is the gold standard for Java implementations. OpenJDK is nearly the same, with Oracle's proprietary code removed. Oracle's older JRocket and IBM's JDK track these closely and initialize the SystemColors identically (and incorrectly).

Name Hex triplet Red Green Blue Hue Satur. Value
activeCaption #000080 0 0 128 240 100 50
activeCaptionBorder #c0c0c0 192 192 192 0 0 75
activeCaptionText #000000 0 0 0 0 0 0
control #dfdfdf 223 223 223 0 0 87
controlDkShadow #4a4a4a 74 74 74 0 0 29
controlHighlight #e6e6e6 230 230 230 0 0 90
controlLtHighlight #ffffff 255 255 255 0 0 100
controlShadow #6b6b6b 107 107 107 0 0 41
controlText #000000 0 0 0 0 0 0
desktop #005c5c 0 92 92 180 100 36
inactiveCaption #808080 128 128 128 0 0 50
inactiveCaptionBorder #c0c0c0 192 192 192 0 0 75
inactiveCaptionText #000000 0 0 0 0 0 0
info #e0e000 224 224 0 60 100 87
infoText #000000 0 0 0 0 0 0
menu #c4c4c4 196 196 196 0 0 76
menuText #000000 0 0 0 0 0 0
scrollbar #c4c4c4 196 196 196 0 0 76
text #ffffff 255 255 255 0 0 100
textHighlight #000000 0 0 0 0 0 0
textHighlightText #c4c4c4 196 196 196 0 0 76
textInactiveText #808080 128 128 128 0 0 50
textText #000000 0 0 0 0 0 0
window #dfdfdf 223 223 223 0 0 87
windowBorder #c4c4c4 196 196 196 0 0 76
windowText #000000 0 0 0 0 0 0

Apache Harmony

Apache's Harmony is an independent full implementation of Java. While it uses different and more esthetically appealing SystemColor defaults, its defaults are still wrong and don't track the OS theme.

Name Hex triplet Red Green Blue Hue Satur. Value
activeCaption #336699 51 102 153 209 66 60
activeCaptionBorder #cccccc 204 204 204 0 0 80
activeCaptionText #ffffff 255 255 255 0 0 100
control #dddddd 221 221 221 0 0 86
controlDkShadow #777777 119 119 119 0 0 46
controlHighlight #eeeeee 238 238 238 0 0 93
controlLtHighlight #ffffff 255 255 255 0 0 100
controlShadow #999999 153 153 153 0 0 60
controlText #000000 0 0 0 0 0 0
desktop #224466 34 68 102 210 66 40
inactiveCaption #888888 136 136 136 0 0 53
inactiveCaptionBorder #cccccc 204 204 204 0 0 80
inactiveCaptionText #dddddd 221 221 221 0 0 86
info #eeeeee 238 238 238 0 0 93
infoText #222222 34 34 34 0 0 13
menu #dddddd 221 221 221 0 0 86
menuText #000000 0 0 0 0 0 0
scrollbar #cccccc 204 204 204 0 0 80
text #ffffff 255 255 255 0 0 100
textHighlight #336699 51 102 153 209 66 60
textHighlightText #ffffff 255 255 255 0 0 100
textInactiveText #dddddd 221 221 221 0 0 86
textText #000000 0 0 0 0 0 0
window #ffffff 255 255 255 0 0 100
windowBorder #000000 0 0 0 0 0 0
windowText #000000 0 0 0 0 0 0

Downloads

Each Java implementation's default SystemColor set is available as a download in the X11 rgb.txt file format:

Further reading

Related articles at NadeauSoftware.com

Web articles

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Nadeau software consulting
Nadeau software consulting