Home > Articles > Programming > Java

Styling Swing Components with Java CSS

Jeff Friesen
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowJeff Friesen

Jeff Friesen

Learn more…

Using Transitions to Simplify JavaFX Animations
Sep 9, 2009
Styling Swing Components with Java CSS
Aug 28, 2009
Show Me the Movie with DirectShow
Apr 28, 2009
Playing Media with Java Media Components
Feb 27, 2009
Creating Java User Interfaces with Project Scene Graph
Feb 10, 2009
Blending Images in Java
Sep 12, 2008
Introduce Animated Cursors to Java GUIs, Part 3
Apr 30, 2008
Introduce Animated Cursors to Java GUIs, Part 2
Apr 18, 2008
Introduce Animated Cursors to Java GUIs, Part 1
Apr 11, 2008
Enhance Java GUIs with Windows Icons
Apr 4, 2008
Taming Mustang, Part 2: Scripting API Tour
Nov 2, 2007
Visit Java's Relatives: Jython and Groovy
May 4, 2007
Generics 101: Mastering the Fundamentals
Mar 23, 2007
Taming Mustang, Part 3: A New Script Engine
Mar 2, 2007
Taming Mustang, Part 1: Collections API
Feb 16, 2007
PCX Meets Image I/O: Creating an Image-Reading Java Plug-in
Dec 29, 2006
Mustang (Java SE 6) Gallops into Town
Oct 20, 2006
GridBagLayout Versus FormLayout
Oct 6, 2006
Laying Out Realistic GUIs the GridBagLayout Way
Sep 29, 2006
Harness the Power of Java's GridBagLayout
Sep 22, 2006
Tools of the Trade: SwingX Meets Swing with New and Extended Components
Aug 4, 2006
Have Fun with the Custom Screensavers Library
Mar 10, 2006
Build Screensavers with a Custom Screensavers Library in Borland C++
Feb 24, 2006
Tools of the Trade: Flash meets Java with Transform SWF and JFlashPlayer
Feb 17, 2006
Tools of the Trade, Part 3: Using the JGoodies Animation Library
Dec 22, 2005
Tools of the Trade, Part 2: Building Graphs with JGraph
Dec 9, 2005
Tools of the Trade, Part 1: Creating PDF documents with iText
Nov 4, 2005
From Literals to Expressions in Java
Aug 16, 2002
Build Your Own Java-Based Email Programs
May 10, 2002
Exploring Java's Network API: URIs and URLs
May 1, 2002
Exploring Java's Network API: Sockets
Apr 19, 2002
Basic Thread Operations in Java
Mar 22, 2002
Working with Streams in Java
Mar 22, 2002
Advanced Tips for More Powerful Tables
Nov 20, 2001
Exploring Swing's Table Component
Nov 20, 2001
Simple Tips for More Powerful Tables
Nov 20, 2001
A Handful of Tips for Swing Programs
Apr 13, 2001
A Trio of Tips for AWT Programs
Apr 13, 2001
Automating Programs with Robots
Apr 13, 2001
Build Your Own Media Player
Apr 13, 2001
Drawing Cubic Curves
Apr 13, 2001
Scaling Images
Apr 13, 2001
Using the Swing API Timers
Apr 13, 2001

Sorry, this author hasn't posted any blogs.

JavaFX provides a Cascading Style Sheets infrastructure for styling nodes. This infrastructure appears to be based upon or influenced by the open source Java CSS project. In this article Jeff Friesen introduces you to Java CSS and shows you how to leverage this Java technology to style your Swing-based user interfaces.

JavaFX provides a Cascading Style Sheets (CSS) infrastructure for styling nodes (objects whose ultimate parent class is javafx.scene.Node). If you're a Swing developer who's jealous of JavaFX's good fortune, you'll be pleased to learn that this infrastructure is available to Swing via the open source Java CSS project.

This article introduces you to Java CSS by showing you where to obtain the Java CSS software and how to compile/run Java programs with this software, and by revealing an online demonstration of this technology. It then presents Java CSS fundamentals in terms of key API classes and concepts, and concludes by showing you how to extend Java CSS through a new property type.

Getting Started with Java CSS

Created by developer Ethan Nicholas, Java CSS is an API that implements CSS for Java GUI components. In addition to blogging about this project in summer 2008, Nicholas has provided a Java CSS tutorial and downloadable code at the project's Java.net-hosted website.

You can download the latest Java CSS binary (css-0.2.zip) and source (css-0.2-src.zip) distributions from the website's "Documents & files" section. You can also browse through the website's Javadoc to learn more about this API.

Unzip css-0.2.zip and you'll discover LICENSE and README files in the unzipped css-0.2 home directory. You'll also discover a lib subdirectory containing css.jar and its dependent beansbinding-1.2.1.jar and TimingFramework-1.0.jar JAR files. (This directory also contains appframework-1.0.3.jar and swing-worker-1.1.jar, which don't appear to be used.)

To compile and run a Java program that relies on Java CSS, you'll need to include css.jar in the classpath; for example, javac -cp css.jar... or java -cp css.jar... (assuming that lib is current). You don't need to also include beansbinding-1.2.1.jar and TimingFramework-1.0.jar in the classpath, because css.jar's MANIFEST.MF file includes these JAR files via its Class-Path attribute.

Nicholas's blog post and project website feature a JNLP-launchable tip calculator application that demonstrates Java CSS. Enter a meal amount into the text field, and use the slider to calculate an appropriate tip (shown via a label). If you select a tip that's too small, the slider's background animates to red. Similarly, it animates to green if you select too large a tip. Figure 1 shows the calculator's styled user interface.

Figure 1 The tip calculator also presents an editor window that lets you modify style sheet settings.

The editor window's user interface presents a list of named application-created style sheets in its right pane, the currently selected style sheet's contents in its left editor pane, an Enable Stylesheet checkbox that applies/removes the editor's style information to/from the tip calculator's user interface, and an Update button that reapplies the editor's styling information to the tip calculator UI.

  • Share ThisShare This
  • Your Account

Discussions

CSS styling with JTable
Posted Dec 16, 2009 06:27 PM by Jasmin123
0 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

David ChisnallCocoa Tip of the Day, 1/29/10
By David Chisnall on January 29, 2010 No Comments

Don't ignore old versions of OS X.

See All Related Blogs

Informit Network