Question

I have a standalone Swing application and I'm using Groovy as programing language.

Trying to apply styles using CSS and searching for some tool for this purpose, I've found CSSBuilder.

The problem is that CSSBuilder comes integrated with Griffon framework, so I cannot use all of its features isolated from Griffon, such as 'cssClass' selector.

Therefore my question is simply: has anyone managed to do something like this?

Was it helpful?

Solution

Just wrote a quick test, and this seems to work:

@GrabResolver( name='codehaus', root='http://repository.codehaus.org' )
@Grab( 'org.codehaus.griffon:cssbuilder:0.4' )
import griffon.builder.css.*
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL

def style = '''* {
  background-color: red;
}

jbutton {
  background-color: blue;
}

.active {
  color: green ;
  font-size: 50%;
}

jlabel {
  color: pink ;
  font-size: 200% ;
}'''

Class klass = javax.swing.JComponent
if( !AbstractSyntheticMetaMethods.hasBeenEnhanced(klass) ) {
   AbstractSyntheticMetaMethods.enhance(klass,[
      "getCssClass": {-> delegate.getClientProperty(ClarityConstants.CLIENT_PROPERTY_CLASS_KEY) },
      "setCssClass": { String cssClass -> delegate.putClientProperty(ClarityConstants.CLIENT_PROPERTY_CLASS_KEY, cssClass) }
   ])
}

new SwingBuilder().edt {
  int count = 0
  def frame = frame( title:'CSS Test', size:[ 300, 300 ], show: true ) {
    borderLayout()
    textlabel = label(text:"Click the button!", constraints: BL.NORTH)
    button(text:'Click Me',
         cssClass: 'active',
         actionPerformed: {count++; textlabel.text = "Clicked ${count} time(s)."; println "clicked"},
         constraints:BL.SOUTH)    
  }
  CSSDecorator.applyStyle( style, frame )
}

The meta-class enhancing code I took from the source of CSSBuilder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top