Griffon as a Quick and Dirty Learning Tool

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.



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.

Posted by Ken Barclay on September 22, 2008 at 05:37 PM PDT #

Couldn't agree with you more, Ken!

Posted by Geertjan on September 22, 2008 at 07:29 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« July 2016