Tutorial: Swing Synth PLAF Template - Part 1
I've been working on a GUI app written is Swing recently, and there's been a request to make it look more modern and attractive. When people are asked to do this, they often start sub-classing components, or calling setters directly. This is the Swing equivalent of HTML inline styles, with a similar set of problems. In HTML you’d use CSS to solve this problem. There are two good methods of styling in Swing. The first is to sub-class an existing pluggable-look-and-feel (PLAF), or to use Synth PLAF and write matching code.
In a previous post I talked about sub-classing a Swing PLAF to get a modified theme.
The Synth PLAF provides a framework for creating your own. You do this by create two things: an XML file that defines coarse grained options such as colours, sizes, insets, and images; and a "painter" that provides a higher degree of control of the rendering of each component.
The code is on Github. You can see a demo using SynthDemoApp.
You specify the XML file programmatically when you choose the PLAF:
SynthLookAndFeel synth = new SynthLookAndFeel();
synth.load(CustomPainter.class.getResourceAsStream("synth.xml"), CustomPainter.class);
UIManager.setLookAndFeel(synth);
There are two routes you can take when creating a theme, you can use images, or a custom painter. The advantage of using images is that it is quick and easy, but you cannot do more complex things, like changing the image based on arbitrary options. The advantage of a custom painter is versatility, but they are technically more difficult. As the former is well documented at Oracle I'm going to talk about custom painters.