Themes for NetBeans Platform Applications
Today I found out, from David Strupl on the NetBeans Platform mailing list, that NetBeans Platform applications can have themes/skins defined, via XML files. That means that I hadn't read the Main class of the NetBeans Platform before, where I would have found the following method:
public static void initUICustomizations() {
if (!CLIOptions.isGui ()) {
return;
}
URL themeURL = null;
boolean wantTheme = Boolean.getBoolean ("netbeans.useTheme") ||
CLIOptions.uiClass != null && CLIOptions.uiClass.getName().indexOf("MetalLookAndFeel") >= 0;
try {
if (wantTheme) {
//Put a couple things into UIDefaults for the plaf library to process if it wants
FileObject fo = FileUtil.getConfigFile("themes.xml"); //NOI18N
if (fo == null) {
// File on SFS failed --> try to load from a jar from path
// /org/netbeans/core/startup/resources/themes.xml
try {
themeURL = new URL("nbresloc:/org/netbeans/core/startup/resources/themes.xml"); //NOI18N
// check whether the file is there:
themeURL.openStream().close();
} catch (IOException ex) {
themeURL = null;
}
} else {
try {
themeURL = fo.getURL();
} catch (FileStateInvalidException fsie) {
//do nothing
}
}
}
} finally {
CoreBridge.getDefault ().initializePlaf(CLIOptions.uiClass, CLIOptions.getFontSize(), themeURL);
}
if (CLIOptions.getFontSize() > 0 && "GTK".equals(UIManager.getLookAndFeel().getID())) { //NOI18N
Util.err.warning(NbBundle.getMessage(Main.class,
"GTK_FONTSIZE_UNSUPPORTED")); //NOI18N
}
StartLog.logProgress("Fonts updated"); // NOI18N
}So, you can define a "themes.xml" file, in certain places, which the NetBeans Platform will then pick up and apply to the application. If you're not using Metal, then you need to provide a special setting to enable themes support.
Here's where I define my "themes.xml" file:
And all the definitions of the content of the file can be found here: http://ui.netbeans.org/docs/ui/themes/themes.html
Some sample themes are included, though they're not necessarily aesthetically pleasing, you can definitely see that something different happened to the application:
Where might themes be useful? Well, here's a themes file that could be pretty handy...
<themeset active="tableRows">
<theme name="tableRows">
<color key="Table.background1" r="170" g="144" b="119"/>
<color key="Table.selectionBackground1" r="190" g="170" b="140"/>
<color key="Table.background2" r="250" g="240" b="210"/>
<color key="Table.selectionBackground2" r="198" g="183" b="157"/>
</theme>
</themeset>
...since it results in the table rows below having alternate colors:
However, if you were using Nimbus, you wouldn't need the above theme, since Nimbus shows alternate colors out of the box:
In the case of Nimbus, and all other non-Metal look-and-feels, you need to (as can be read in the code at the top of this article) set "-J-Dnetbeans.useTheme=true" in the "platform.properties" of the application, if you want to use a themes file:
run.args.extra=--laf com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel -J-Dnetbeans.useTheme=true
The above will then enable a Nimbus look-and-feel to have a limited subset of theme definitions being available to it, such as these for enlarging the font:
Pretty cool that themes support is available and that it is fairly easy to apply them in this way.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)




Comments
Tim Sparg replied on Wed, 2010/04/21 - 3:27am
Is it possible to change the look and feel of the platform at runtime?
I tried
UIManager.setLookAndFeel("nimbus");but it didnt seem to work for me, is there another way to do it?
Robert Piesnikowski replied on Thu, 2010/04/22 - 11:20am
Hello Sparg.
I used changing Look & Feel at runtime but only in Swing application. Neverheless if you use
UIManager.setLookAndFeel("yourL&F");You have to remeber to revalidate all controls in JFrame. To do this you need to have reference to your JFrame window and after setting new L&F you simply revalidate all inside window.
Bu Gu replied on Sun, 2010/08/01 - 1:49am
Matt Coleman replied on Fri, 2011/11/18 - 12:43am
this is just awesome..great work netbeans
buffalo website design