O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

440
|
Chapter 11, Native Integration and Packaging
#87 Create an Inverse Black-and-White Theme
HACK
A Black-and-White Theme
A Metal theme is defined by a series of colors and fonts. The most impor-
tant ones are the three primary and three secondary colors. These define the
standard set of colors used for every widget on screen. Certain components
have additional colors, like the text selection, but almost everything is based
on these six. Example 11-9 is a theme that uses only white and black. It is
useful for embedded devices that can’t afford the hardware or memory
requirements of a color display.
Two of the primary colors are all white, and the third is black. The three sec-
ondary colors are just the reverse: two black and one white. This introduces
extra contrast. Each color controls a different part of the component. The
MetalTheme documentation is very sparse, so the only way to know what a
particular color will do is to try it. Without the theme, the screen will look
like Figure 11-17.
With the
InverseTheme installed, the window will look like Figure 11-18.
Example 11-9. A black-and-white Metal theme
import javax.swing.plaf.metal.*;
import javax.swing.plaf.ColorUIResource;
public class InverseTheme extends DefaultMetalTheme {
protected ColorUIResource getPrimary1( ) {
return new ColorUIResource(255,255,255);
}
protected ColorUIResource getPrimary2( ) {
return new ColorUIResource(0,0,0);
}
protected ColorUIResource getPrimary3( ) {
return new ColorUIResource(255,255,255);
}
// component borders
protected ColorUIResource getSecondary1( ) {
return new ColorUIResource(0,0,0);
}
// selected components (button down state)
protected ColorUIResource getSecondary2( ) {
return new ColorUIResource(0,0,0);
}
// component backgrounds
protected ColorUIResource getSecondary3( ) {
return new ColorUIResource(255,255,255);
}
Create an Inverse Black-and-White Theme #87
Chapter 11, Native Integration and Packaging
|
441
HACK
Setting the six colors gets us most of the way, but there are still some prob-
lems. In particular, the label is hidden and the text field looks wrong when
you select it. To correct these, you’ll need to set three more colors:
//for label text
public ColorUIResource getSystemTextColor( ) {
return new ColorUIResource(0,0,0);
}
// background of selected text
public ColorUIResource getTextHighlightColor( ) {
return new ColorUIResource(0,0,0);
}
// foreground of selected text
public ColorUIResource getHighlightedTextColor( ) {
return new ColorUIResource(255,255,255);
}
Figure 11-17. Window with no changes
Figure 11-18. Window with an inverse theme
442
|
Chapter 11, Native Integration and Packaging
#87 Create an Inverse Black-and-White Theme
HACK
The SystemTextColor is used for labels and titled borders, so you have to set
this to black as well. The highlighted text, as well as the background of the
highlight, are controlled by the
HighlightedTextColor
and
TextHighlightColor
,
respectively (confusing, I know).
The only remaining problem is the menus. They have their own set of prop-
erties, which you can also set:
public ColorUIResource getMenuBackground( ) {
return new ColorUIResource(255,255,255);
}
public ColorUIResource getMenuForeground( ) {
return new ColorUIResource(0,0,0);
}
public ColorUIResource getMenuSelectedBackground( ) {
return new ColorUIResource(0,0,0);
}
public ColorUIResource getMenuSelectedForeground( ) {
return new ColorUIResource(255,255,255);
}
With all of the colors set, you are ready to load up the theme. All you have
to do is call
MetalLookAndFeel.setCurrentTheme( ) before you create any
Swing components.
public static void main(String[] args) {
MetalLookAndFeel.setCurrentTheme(new InverseTheme( ));
//.. set up Swing components now
}
If you set it as the first line of the main( ) method in the demonstration pro-
gram from the previous hack, you will see your themed window as in
Figure 11-19.
Figure 11-19. A better inverse theme

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required