Styling a Griffon application - No Fluff Just Stuff

Styling a Griffon application

Posted by: Andres Almiray on June 10, 2009

Wouldn't it be great to style a Griffon application using CSS? If you do a quick google search for "Swing css" you will find plenty of information on how to do it (even a link back to this blog -> CSS + Swing + Groovy? you bet!), what strikes me as odd is that Swing Clarity is not among the first hits. This small library comes from the same people that brought you ajaxian.com, yup that's true, Ben & Dion. I remember clearly watching Ben demonstrating the library back at JavaOne 2007, it's been a while...

Back on topic, Swing Clarity borrows behavior from popular Javascript libraries like Prototype and JQuery, it even provides JQuery-like CSS selector methods -> $() and $$(). Creating a Groovy friendly wrapper for this library is quite simple, as a matter of fact there is a new builder on the works: CSSBuilder. This class is responsible for handling a new attribute that can be placed on any SwingBuilder node: cssClass, this attribute has the same effect as class has on an HTML element. Another thing the builder does is inject the following methods to the java.awt.Container class
  • $(String name) -> finds a JComponent by name
  • $s(String... names) -> finds all JComponents whose names match the parameters
  • $$(String selector) -> finds all JComponents using a CSS selector
It also is able to inject these methods to a Griffon controller, those versions take a java.awt.Container as preceding parameter. Enough theory, let's look at this puppy in action, here is a screenshot of a sample application



I'm sorry for the bright colors, I didn't say the image would be pretty, did I? ;-) This little application has been styled using the following stylesheet
This are the required steps for styling the application

1. Copy CSSBuilder and its dependencies into your app's lib directory [http://svn.codehaus.org//griffon/builders/cssbuilder/trunk/]

2. Configure the builder to be used with Griffon's CompositeBuilder, your griffon-app/conf/Builder.config should look like
3. Make sure the style is applied after all the views have been created, place the following code at griffon-app/lifecycle/Startup.groovy
, be mindful that the stylesheet file will be read inside the EDT, the UI will be updated inside the EDT as well. 4. Finally, let's configure a simple view. You have the choice of setting a name: property on a target component or using the new cssClass: attribute
CSSBuilder is still in the early stages, the default CSS properties handled by Swing Clarity are but a small set of CSS2, there are also a few others that can be created specifically for Swing. A Griffon plugin will be available once CSSBuilder makes its first release (hopefully soon!)

A big shoutout goes to Ben & Dion for making this great library!

Keep on Groovying!
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »