Geertjan's Blog

  • September 22, 2008

Griffon as a Quick and Dirty Learning Tool

Geertjan Wielenga
Product Manager
It's not like I'm constantly looking for useful scenarios for Griffon and then blogging about it. It's more like I keep tripping over them accidentally, all the time. I just can't help it! Here's the latest one. Let's say you watched Roumen's very cool Javalobby NetBeans Visual Library Presentation. "Cool, you think, let's learn about that." At some point you put together a piece of code like this:
public class GraphSceneImpl extends GraphScene {
private LayerWidget mainLayer
public GraphSceneImpl() {
mainLayer = new LayerWidget (this)
addChild (mainLayer)
createWidget(mainLayer, LayoutFactory.SerialAlignment.JUSTIFY, 50, 50)
createWidget(mainLayer, LayoutFactory.SerialAlignment.JUSTIFY, 20, 20)
private static void createWidget(LayerWidget layer, LayoutFactory.SerialAlignment align, int x, int y) {
Widget widget = new Widget(layer.getScene())
widget.setBorder(BorderFactory.createResizeBorder(8, Color.BLACK, false))
widget.setLayout(LayoutFactory.createVerticalFlowLayout(align, 0))
widget.setPreferredLocation(new Point(x, y))
widget.setPreferredBounds(new Rectangle(200, 200))
protected Widget attachNodeWidget(Object node) {return null}
protected Widget attachEdgeWidget(Object edge) {return null}
protected void attachEdgeSourceAnchor(Object edge, Object oldSourceNode, Object newSourceNode) {}
protected void attachEdgeTargetAnchor(Object edge, Object oldTargetNode, Object newTargetNode) {}

The widgets, border factory, action factory, and so on, all come from the org.netbeans.api.visual package, which is exactly the point: the Visual Library handles all these concerns for you, you can simply reuse them in your own code. Now, the question is how do you actually try out this code above?

For a start, you're a sloppy programmer, so you've left out all the semi-colons. Hurray. That's Groovy. Normally, to show the above Visual Library scene in a Java application, you'd need to create a Java application, add a container like a JFrame or a JPanel, and then add a JScrollPane, mess around with the ViewportView to which you add the scene's JComponent, via scene.createView(). Phew! That's a lot of work. How about doing it like this instead?:

That's literally the entire view of my application! The frame, the JScrollPane, the JComponent, everything, it's all there... just in a Groovy way, within the context of a simple Griffon application:

Now, when I run the application, I can immediately see, for three lines of code shown above (only one of which I wrote myself, since the other two are part of the output from 'griffon create-app'!), that my code has been correctly hooked up and that it's doing something useful. The resize action is working and so is the move action. The resize border is also how I'd expect it to be:

Furthermore, I can also deploy my small application as an applet, where it works exactly the same way:

In short, Griffon provides a cool prototyping & learning environment which can be used, for example, to focus on the specific APIs you're learning about... because Griffon handles everything else around it for you, from the creation of the application, to its structure, to giving you a placeholder for coding the few Groovy lines needed for hooking the API class into the Griffon application. Pretty cool.

Join the discussion

Comments ( 2 )
  • Ken Barclay Tuesday, September 23, 2008


    I locked on to 'Learning Tool' in the title of this article. I am presently exploring Groovy as a first programming language. Today's crop of students look for something more sexy than simple text based UIs. Groovy in general, SwingBuilder, GraphicsBuilder and now Griffon make it possible to introduce programming in a contemporary and fun manner.

  • Geertjan Tuesday, September 23, 2008

    Couldn't agree with you more, Ken!

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.