concept SwingBuilder in category groovy

appears as: SwingBuilder, SwingBuilder
Groovy in Action, Second Edition

This is an excerpt from Manning's book Groovy in Action, Second Edition.

SwingBuilder is simple in appearance but very elaborate inside. Its methods don’t only create and connect the plain Swing widgets that represent views but also give access to those objects that Swing uses for gluing together the final GUI-like actions, models, layout managers, and constraints.

Layout management with SwingBuilder can be achieved in two ways: by setting the according properties on the widgets themselves or by using nested method calls.

We begin with the first option, which works without any layout-specific treatment in SwingBuilder. This is shown with an example that uses Swing’s BorderLayout with JButtons in figure 11.6.

Figure 11.6. Swing BorderLayout defined through SwingBuilder

Using SwingBuilder, it was easy to integrate that dialog in a script. Listing 11.10 contains the snippet that achieves this. It follows the same strategy that you’ve already seen for other builders: it creates a builder instance and calls methods on it with attached closures that make up the nesting structure and argument maps that further define properties of the product.

Listing 11.10. Simple password dialog with SwingBuilder
import groovy.swing.SwingBuilder swing = new SwingBuilder() frame = swing.frame(title:'Password') { passwordField(columns:10, actionPerformed: { event -> println event.source.text // any further processing is called here System.exit(0) } ) } frame.pack() frame.visible = true

The example shows two idiosyncrasies of SwingBuilder:

  • For constructing a JFrame the according method name is frame, not jFrame as one may expect. This allows reusing code with builders for other widget sets like AWTBuilder (not yet available), SWTBuilder, or ULCBuilder.[6]

    6 ULC is a server-side widget set that allows writing web applications on the server in a Swing-equivalent manner. The UI is presented on the client through an application-independent UI engine. ULCBuilder enables you to write Groovy ULC applications analogous to writing Swing applications with SwingBuilder. See www.canoo.com/ulc.

  • 6 ULC is a server-side widget set that allows writing web applications on the server in a Swing-equivalent manner. The UI is presented on the client through an application-independent UI engine. ULCBuilder enables you to write Groovy ULC applications analogous to writing Swing applications with SwingBuilder. See www.canoo.com/ulc.

    Making Java Groovy: Foreword by Guillaume Laforge

    This is an excerpt from Manning's book Making Java Groovy: Foreword by Guillaume Laforge.

    In this script I’m importing three classes from the Java standard library. The first two imports use the as operator to build an alias for the respective classes. That way the code that uses BorderLayout and WindowConstants can just write BL or WC instead. I’m also adding in the ImageIcon class, which will hold the image returned by Google Chart. The import from the Groovy library is SwingBuilder, which will be used to construct the Swing UI.

    In the case of SwingBuilder you invoke methods that don’t exist on the builder but that are translated to the corresponding Swing API. For example, by calling the frame method you’re actually instantiating the JFrame class. Giving it a map-like argument of visible:true corresponds to calling the setVisible method with a true argument.

    Here’s the code that uses the builder. Each method not in SwingBuilder is translated to the proper method call on the Swing library class:

    SwingBuilder.edt {
        frame(title:'Hello, World!', visible:true, pack: true,
            defaultCloseOperation:WC.EXIT_ON_CLOSE) {
                label(icon:new ImageIcon("$base$qs".toURL()),
                    constraints:BL.CENTER)
        }
    }

    The edt method on SwingBuilder builds a GUI using the event dispatch thread. It takes a closure as an argument, and this is where the fun starts. The first statement inside the closure is a call to the frame method, but the fact is, there’s no frame method in SwingBuilder. The builder’s metaclass intercepts that call (via methodMissing) and interprets it as a request to instantiate the javax.swing.JFrame class. The frame method here lists a series of map entries, which are intended to supply values for the title, visibility, and close operation on the JFrame. The builder interprets them as calls to setTitle, setVisible, and setDefaultCloseOperation on the JFrame instance.