|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
Ethan Nicholas's Blog
Introducing Java CSSPosted by enicholas on July 17, 2008 at 02:12 PM | Permalink | Comments (18)How people configure Swing todayIf you've ever written a non-trivial Swing program, you've probably written code that looks a lot like this: JSlider slider = new JSlider(0, 100, currentValue); slider.setMinorTickSpacing(5); slider.setMajorTickSpacing(25); slider.setPaintTicks(true); slider.setForeground(DEFAULT_FOREGROUND); That's an awful lot of configuration for one component, and worse: it's all compiled code, and therefore relatively inaccessible. The more experienced programmers in the audience are going to immediately (and quite rightly) complain about the setForeground() call -- that should be installed into the UIDefaults table -- and we should probably be using constants instead of those magic numbers. But then we, in a sense, end up with an even bigger mess. Some of the
slider's configuration comes from the UIDefaults table, some comes from
constants defined well away from the slider itself, and some (like
Even-more-experienced coders will now suggest another change -- that we put the constants into a configuration file of some kind, rather than into code. So we perhaps write a configuration file containing something along the lines of: default.font = Dialog-12 default.foreground = #000000 mySlider.minorTickSpacing = 5 mySlider.majorTickSpacing = 25 mySlider.paintTicks = true Problem solved! Right? Well, sort of. You've still got to write code to
process this configuration file. You've got to work out how the values defined
in the file actually get applied to components.
I've seen configuration files along these lines (though the details of course differ) in many different Swing programs, and I don't think they're uncommon. I've written some myself. But there's a real problem with this approach -- you're reinventing the wheel. What you've created is in effect a very simple and limited stylesheet language. And if you're going to do that, why not just use a real stylesheet language? Using CSS insteadThere already exists a powerful, flexible stylesheet language that millions of people are familiar with. It's called CSS, and various (mostly very poor) implementations already exist for Java. With a proper CSS engine, you wouldn't need to bury the configuration in your code or burn it into proprietary configuration files. The configuration above might be expressed as: * {
font: Dialog-12;
foreground: black;
}
JSlider#mySlider {
minorTickSpacing: 5;
majorTickSpacing: 25;
paintTicks: true;
}
Admittedly, in this simple example there's not a clear benefit to using CSS instead of a proprietary file -- the syntax is a bit different, but the two files are about equally complex. The real benefit of CSS comes from all of the additional power it offers. Want to only affect sliders which appear under MyColorChooser? No problem, use
the selector What about even more complex selectors, such as "JSliders which are direct
children of a MyPanel appearing in the currently active window, but only when
they are set to their maximum value"? No problem, with a slight syntax
extension The Java CSS EngineI mentioned above that the existing CSS implementations for Java are generally very poor. That's why I ended up creating a new one, which (hopefully) far exceeds previous CSS engines in many respects. Click the launch button to see a simple demo of my CSS engine:
For comparison, here is the exact same application without a stylesheet:
The demo application also includes a stylesheet editor window, which allows you to update the stylesheet or turn it off altogether:
The stylesheet specifies some interesting rules, such as
JSlider#tip:{value <= 10} {
background: red !over 0.3s;
}
The One other unusual feature of this rule is the ConclusionJava CSS is a powerful, flexible engine for styling Swing components, supporting very powerful selectors, tons of pseudoclass states, and advanced features like animated state changes. For full documentation or to download Java CSS, please visit the Java CSS Home Page. |
August 2008
Search this blog:CategoriesCommunity: Java ToolsCommunity: JavaDesktop Community: Mac Java Community Deployment J2SE JavaOne Performance Programming Security Virtual Machine Archives
July 2008 Recent EntriesArticlesIntroducing JAXX: A New Way to Swing All articles by Ethan Nicholas » | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|