-
Notifications
You must be signed in to change notification settings - Fork 235
How to style Swing components
Available since WebLaF v1.2.9 release, updated for WebLaF v1.2.12 release
Requires weblaf-ui module and Java 6 update 30 or any later to run
As you might know - all Web*
components coming along with WebLaF have a lot of additional convenience methods to access various exclusive component and library features. One of such features is styling system allowing you to re-style any components to look the way you want them to be.
To simplify re-styling of each specific component all Web*
components implement Styleable
interface and provide multiple bridge methods leading into StyleManager
. Also all Web*
components provide an additional constructor where you can put StyleId
for that specific component instance on its creation. Here are a few examples of how common re-styled button would look like in code:
final WebButton button = new WebButton ( StyleId.of ( "custom" ) );
final WebButton button = new WebButton ();
button.setStyleId ( StyleId.of ( "custom" ) );
Though that would force you to use WebLaF imports directly which is not good if WebLaF is simply one of Look and Feel options your application has and even in that case you might still want to customize some UI elements to look better differently.
This is where an additional option of styling base Swing components kicks in:
final JButton button = new JButton ();
button.putClientProperty ( "styleId", "custom" );
As soon as you put this property into component it will be updated with according style if one can be found.
You can also provide StyleId.of ( "custom" )
instance instead of "custom" and use StyleId.STYLE_PROPERTY
reference instead of "styleId", here is the same example using StyleId
:
final JButton button = new JButton ();
button.putClientProperty ( StyleId.STYLE_PROPERTY, StyleId.of ( "custom" ) );
Note that will force you to use WebLaF imports which might not be acceptable in some cases. Though you can use references to StyleId
instances you might store separately or references to default styles defined as static fields in StyleId
class itself.
There is also one more way to set StyleId
into any JComponent
:
StyleId.of ( "custom" ).set ( jComponent );
This is the same as providing it in Web
-component constructor or using it through client properties of JComponent
.
Since some styles are complex and contain their own child styles (for instance complex components like JFileChooser
) there is a "parent" option that can be specified in a similar way:
final JPanel panel = new JPanel ();
final JButton button = new JButton ();
button.putClientProperty ( "styleId", "custom" );
button.putClientProperty ( "parent", panel );
This is required if you are using a complex structure of styles instead of a flat list. Plain "parent" string can also be replaced with StyleId.PARENT_STYLE_PROPERTY
reference here.
Another way of defining the same style is using StyleId
directly:
final JPanel panel = new JPanel ();
final JButton button = new JButton ();
button.putClientProperty ( "styleId", StyleId.of ( "custom", panel ) );
Or using utility ChildStyleId
class:
final JPanel panel = new JPanel ();
final JButton button = new JButton ();
button.putClientProperty ( "styleId", ChildStyleId.of ( "custom" ).at ( panel ) );
This utility class can be useful if you define ChildStyleId
instance separately and provide parent in each specific case separately. As an example it is widely used in complex WebLaF components like file chooser or root pane - there are a lot of ChildStyleId
instances defined in StyleId
as static fields for default styles referencing convenience.
And of course you can also use StyleId.set ( JComponent )
method:
ChildStyleId.of ( "custom" ).at ( panel ).set ( jComponent );
It doesn't matter which one you use as each of these approaches will have exactly the same result.
Reference-less approach based on simple String
s, while less convenient, might be better for projects where you don't want to have any references to 3rd-party library classes.
If you found any mistakes or inconsistency in this article, feel that it is lacking explanation or simply want to request an additional wiki article covering some topic:
I will do my best to answer and provide assistance as soon as possible!