|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionBesides enhanced looks and advanced features, one of the best things about Swing is its pluggable look and feel (PLAF). PLAF architecture allows seamless changes in the appearance of an application and the way an application interacts with the user. However, designing and developing a PLAF is much more exhaustive and complex. On the other hand, themes provide a simple alternative to change look and feel of the swing application. Themes are easier to implement and they enhance the visual appeal of the application UI using the default Java look and feel. Theme mechanism allows a developer to easily specify the default colors, fonts and icons used by the look and feel (L&F). It allows developers to write their own themes; giving them a choice to show their application GUI the way they want it, rather than depending on the defaults provided by the system. This article discusses how to use different themes for Swing's default "Metal" look and feel. The metal look and feel, which is also known as Java look and feel is supported on all Java 2 platforms. Metal ThemesDefault theme of Swing's metal look and feel is "Steel". The class behind Steel theme is - Any application, which intends to use its own theme for the UI can subclass the DefaultMetalTheme.
Built-in ThemesBesides Java's default Steel theme, this application uses two more built-in themes - White Satin and Moody Blues. These built-in themes are implemented as classes. Each one of them extends the class import javax.swing.plaf.*;
import javax.swing.plaf.metal.*;
public class MoodyBlueTheme extends DefaultMetalTheme
{
public String getName() { return "Moody Blues"; }
// blue shades
private final ColorUIResource primary1 =
new ColorUIResource(0x0, 0x33, 0x99);
private final ColorUIResource primary2 =
new ColorUIResource(0xD0, 0xDD, 0xFF);
private final ColorUIResource primary3 =
new ColorUIResource(0x0, 0x99, 0xFF);
private final ColorUIResource secondary1 =
new ColorUIResource(0x6F, 0x6F, 0x6F);
private final ColorUIResource secondary2 =
new ColorUIResource(0x9F, 0x9F, 0x9F);
private final ColorUIResource secondary3 =
new ColorUIResource(0x1f, 0x7f, 0xDC);
// the functions overridden from the base
// class => DefaultMetalTheme
protected ColorUIResource getPrimary1() { return primary1; }
protected ColorUIResource getPrimary2() { return primary2; }
protected ColorUIResource getPrimary3() { return primary3; }
protected ColorUIResource getSecondary1() { return secondary1; }
protected ColorUIResource getSecondary2() { return secondary2; }
protected ColorUIResource getSecondary3() { return secondary3; }
}
The Moody Blues theme only changes the default primary and secondary colors of the default Java look and feel. You can also override the fonts' methods to change the fonts, as it is exemplified in the White Satin theme. Once the user selects a particular theme from the Themes menu, an object of that particular theme's class is created and the entire UI is updated to reflect the new theme. The code for this can be written as - // user selects theme - Moody Blues
MetalTheme theme = new MoodyBlueTheme();
// set the chosen theme
MetalLookAndFeel.setCurrentTheme(theme);
// Show name of the theme as window title
this.setTitle(theme.getName());
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
}
catch(Exception e)
{
System.out.println(e);
}
Custom ThemesCustom themes are handled by the class ConstructorThe constructor of public CustomTheme(InputStream is)
{
defaultColors();
defaultFonts();
loadProperties(is);
}
Reading the custom theme fileTheme file is stored as a Java properties file in the specified format. The format of this theme file is given in the "Help" of this application. Also, a sample theme file - "gray.theme" is given with this application. It is very simple and straightforward to create your own themes using this format. The method private void loadProperties(InputStream is)
{
Properties prop = new Properties();
// load the Properties
try{
prop.load(is);
}catch(IOException e)
{
System.out.println(e);
}
// get theme name
String szTemp = prop.getProperty("name");
if(null != szTemp)
{
szThemeName = szTemp;
}
// get primary colors
szTemp = prop.getProperty("primary1");
if(null != szTemp)
{
primary1 = parseColor(szTemp);
}
// read more properties
// ....................................
}
Parsing color and fontThe other two functions The process of reflecting the selected theme in UI is same for the custom theme as well, which is already discussed earlier. Decorative Frames & DialogsThe following lines are added in the // For JDK 1.4 only.
// Comment these lines for JDK 1.2 & 1.3
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
If you are using JDK 1.2 or JDK 1.3, you will have to comment these two lines to compile the code without any error. No other change is required in rest of the code. I have successfully compiled and executed the code with JDK 1.2 and JDK 1.3 after commenting these two lines. Obviously, the decorated look and feel of the frames and dialogs will not be visible. The demo available with this article has been developed with JDK 1.4 and you will need compatible version of JVM to run it. If you are using MS Windows, you can simply run the demo by double-clicking the "theme.jar" file. Other ResourcesWhile developing this application, I have referred to the demo code that is provided with the JDK 1.4 kit. Here are some other resources that you might find interesting and useful -
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||