(Quick Reference)

7.2 Special Nodes - Reference Documentation

Authors: Andres Almiray

Version: 1.2.0

7.2 Special Nodes

The rule of thumb to find out the node name of a Swing class is this:
  • drop the first J from the class name
  • uncapitalize the next character

Examples

  • JButton => button
  • JLabel => label

This rules apply to all Swing classes available in the JDK. There are a few additional nodes that provide a special function, which will be explained next.

7.2.1 Application

Provided by: Swing plugin

This node defines a top level container depending on the current running mode. It it's STANDALONE or WEBSTART it will create a Window subclass according to the following rules:

  • class name defined in app.config.application.frameClass (configured in Application.groovy)
  • JXFrame if SwingX is available
  • JFrame if all others fail

There's a slight change for the APPLET run mode, the container returned for the first invocation of the application node will be the applet itself, for all others the previous rules apply.

Of all the properties suggested by the default template you'll notice iconImage and iconImages. The first property is a standard property of JFrame. It's usually defines the icon to be displayed at the top of the frame (on platforms that support such setting). The second property (iconImages) is a Jdk6 addition to java.awt.Window. This property instructs the window to select the most appropriate icon according to platform preferences. Griffon ignores this setting if running in Jdk5. This property overrides the setting specified for iconImage if its supported in the current Jdk and platform.

7.2.2 Container

Provided by: SwingBuilder

This is a pass through node that accepts any UI component as value. This node allows nesting of child content. It's quite useful when what you need is to embed a custom component for which a node is not available, for example

container(new MyCustomPanel()) {
    label 'Groovy is cool'
}

7.2.3 Widget

Provided by: SwingBuilder

This is a pass through node that accepts any UI component as value. As opposed to container, this node does not allow nesting of child content. It's quite useful when what you need is to embed a custom component for which a node is not available, for example

widget(new MyCustomDisplay(), title: 'Groovy') {

7.2.4 Bean

Provided by: SwingBuilder

This is a catch-all node, it allows you to set properties on any object using the builder syntax, for example setting up bindings on a model

textField columns: 20, id: username
bean(model, value: bind{ username.text })

The previous code is equivalent to

textField columns: 20, text: bind('value', target: model)

7.2.5 Noparent

Provided by: SwingBuilder

Child nodes are always attached to their parents, there are times when you explicitly don't want that to happen. If that is the case then wrap those nodes with noparent

panel {
    gridLayout(cols: 2, rows: 2)
    button('Click 1', id: b1')
    button('Click 2', id: b2')
    button('Click 3', id: b2')
    button('Click 4', id: b4')

// the following line will cause the buttons // to be reordered // bean(button1, text: 'Click 11')

noparent { // this is safe, buttons do not change places bean(button1, text: 'Click 11') } }

7.2.6 Root

Provided by: Griffon

Identifies the top level node of a secondary View script. View scripts are expected to return the top level node, however there may be times when further customizations prevent this from happening, for example wiring up a custom listener. When that happens the result has to be made explicit otherwise the script will return the wrong value. Using the root() node avoids forgetting this fact while also providing an alias for the node.

Secondary view script named "SampleSecondary"

root(
    tree(id: 'mytree')
)

mytree.addTreeSelectionModel(new DefaultTreeSelectionModel() { … })

Primary view script named "SampleView"

build(SampleSecondary)
application(title: 'Sample') {
    borderLayout()
    label 'Options', constraints: NORTH
    widget root(SampleSecondary)
}

This node accepts an additional parameter name that can be used to override the default alias assigned to the node. If you specify a value for this parameter when the node is built then you'll need to use it again to retrieve the node.

7.2.7 MetaComponent

Provided by: Griffon

Enables the usage of a meta-component as a View node. Meta-components are MVC groups that contain additional configuration, for example

mvcGroups {
    'custom' {
        model      = 'sample.CustomModel'
        view       = 'sample.CustomView'
        controller = 'sample.CustomController'
        config {
            component = true
            title = 'My Default Title'
        }
    }
}

The metaComponent() node instantiates the MVC group and attaches the top node from the groups' View member into the current hierarchy. Using the previous group definition in a View script is straight forward

metaComponent('custom', title: 'Another Title')