Substance look and feel - getting started
This document describes the steps to create a sample Swing
application and run it under Substance look and feel.
Since Substance requires JDK 5.0 or higher, if you don't
have such an installation on your machine, you need to download
it from this site and install
it. The command-prompt examples below assume that java
executable is in the path. This executable is part of JRE
(under bin folder) as well as part of JDK (under bin
folder). Consult your OS manual on how to add the relevant folder to the
path. Alternatively, you can put the entire path to the java
executable in the scripts below.
After you have JDK 5.0+ installed on your machine, create
the following Walkthrough.java class:
public class Walkthrough extends JFrame {
public Walkthrough() {
super("Sample app");
this.setLayout(new FlowLayout());
this.add(new JButton("button"));
this.add(new JCheckBox("check"));
this.add(new JLabel("label"));
this.pack();
this.setSize(this.getPreferredSize());
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Walkthrough w = new Walkthrough();
w.setVisible(true);
}
}
This is a simple frame (that does nothing) with a button, a checkbox and a label.
You can create this class in your favourite IDE or in any text editor. Once
this class is created, compile it. If you're using an IDE, consult the IDE
help on the compilation process. If you're using a simple text editor, you
can compile this class by using:
javac Walktrough.java
If you have problems, consult the online help for javac compiler. The compiled
Walktrough.class will be created in the same folder as your Walkthrough.java.
In order to run it, use:
java -cp . Walkthrough
You will see the following frame under the default Ocean look and feel:
In order to run the same frame under Substance look and feel, you have
the following options:
- Start your VM with -Dswing.defaultlaf=org.jvnet.substance.SubstanceLookAndFeel
- UIManager.setLookAndFeel(new SubstanceLookAndFeel());
- UIManager.setLookAndFeel("org.jvnet.substance.SubstanceLookAndFeel");
The first option doesn't require any code changes. Run the
following script:
java -Dswing.defaultlaf=org.jvnet.substance.SubstanceLookAndFeel -cp . Walkthrough
You will see the following exception:
Exception in thread "main" java.lang.Error: can't load org.jvnet.substance.SubstanceLookAndFeel
at javax.swing.UIManager.initializeDefaultLAF(Unknown Source)
at javax.swing.UIManager.initialize(Unknown Source)
at javax.swing.UIManager.maybeInitialize(Unknown Source)
at javax.swing.UIManager.getUI(Unknown Source)
at javax.swing.JPanel.updateUI(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JPanel.<init>(Unknown Source)
at javax.swing.JRootPane.createGlassPane(Unknown Source)
at javax.swing.JRootPane.<init>(Unknown Source)
at javax.swing.JFrame.createRootPane(Unknown Source)
at javax.swing.JFrame.frameInit(Unknown Source)
at javax.swing.JFrame.<init>(Unknown Source)
at Walkthrough.<init>(Walkthrough.java:8)
at Walkthrough.main(Walkthrough.java:21)
This means that the org.jvnet.substance.SubstanceLookAndFeel class
in not found in the classpath. This class is the main class of the Substance
look and feel and is located in the substance.jar that you need to
download from the Documents & Files section of the
Substance project page.
For this example, we assume that the substance.jar is located
under C:/temp folder. In order to run the frame under Substance,
use the following script:
java -Dswing.defaultlaf=org.jvnet.substance.SubstanceLookAndFeel -cp .;C:/temp/substance.jar Walkthrough
The result is the same frame under Substance look and feel:
The other two options for setting Substance require changing the
code. Go back to your Java editor and replace the main method by:
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
} catch (UnsupportedLookAndFeelException ulafe) {
System.out.println("Substance failed to set");
}
Walkthrough w = new Walkthrough();
w.setVisible(true);
}
In order to compile the new Walktrough.java, you need to add the substance.jar
to the build path. Consult your IDE help if you're using IDE. For command-prompt compilation,
use the additional -cp flag:
javac -cp c:/temp/substance.jar Walktrough.java
Now you can run your application without the -Dswing.defaultlaf JVM flag, but
you still need to specify the location of the substance.jar as before:
java -cp .;C:/temp/substance.jar Walkthrough
If you don't want to create an explicit dependency on the Substance
classes in your code, change your main method to:
public static void main(String[] args) {
try {
UIManager
.setLookAndFeel("org.jvnet.substance.SubstanceLookAndFeel");
} catch (UnsupportedLookAndFeelException ulafe) {
System.out.println("Substance failed to set");
} catch (ClassNotFoundException cnfe) {
System.out.println("Substance not found");
} catch (InstantiationException ie) {
System.out.println("Substance failed to instantiate");
} catch (IllegalAccessException iae) {
System.out.println("Access denied");
}
Walkthrough w = new Walkthrough();
w.setVisible(true);
}
You can run the application the same way as before.
One of the many Substance customizations (follow the links below) is
changing a color theme. In order to change the current theme, you can
- Set substancelaf.theme parameter of the VM, for example
-Dsubstancelaf.theme=org.jvnet.substance.theme.SubstanceBrownTheme
- Call SubstanceLookAndFeel.setCurrentTheme() with one of the
above themes, for example SubstanceLookAndFeel.setCurrentTheme(new
SubstanceBrownTheme())
- Call SubstanceLookAndFeel.setCurrentTheme() with the fully
qualified name of one of the above themes, for example
SubstanceLookAndFeel.setCurrentTheme("org.jvnet.substance.theme.SubstanceBrownTheme")
If you use one of the first two options after your application has started,
don't forget to call:
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
SwingUtilities.updateComponentTreeUI(...);
Using the first option, change the launch command to
java -Dsubstancelaf.theme=org.jvnet.substance.theme.SubstanceEbonyTheme -cp .;C:/temp/substance.jar Walkthrough
Using the second option, change your main() method to:
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
SubstanceLookAndFeel.setCurrentTheme(new SubstanceEbonyTheme());
UIManager.setLookAndFeel(new SubstanceLookAndFeel());
} catch (UnsupportedLookAndFeelException ulafe) {
System.out.println("Substance failed to set");
}
Walkthrough w = new Walkthrough();
w.setVisible(true);
}
And run using
java -cp .;C:/temp/substance.jar Walkthrough
Where to go from here?