To wrap up our overview of AST transformations, let's finish by speaking about two transformations very useful to Swing developers: @Bindable and @Vetoable. When creating Swing UIs, you're often interested in monitoring the changes of value of certain UI elements. For this purpose, the usual approach is to use JavaBeans PropertyChangeListeners to be notified when the value of a class field changes. You then end up writing this very common boiler-plate code in your Java beans:

AST変換の概要を掴んでいただくため、Swing開発者にとって便利な @Bindable と @Vetoable という二つの変換から話し終えることにしましょう。SwingのUIを作成した際、UI要素の値の変更を監視したいと思うでしょう。この目的のためによく使われる手法は、JavaBeansのPropertyChangeListenersを使ってクラスフィールドの変更を通知させる方法です。最終的には、よくあるお決まりのJava Beansのコードを書くことになるでしょう。

import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;

public class MyBean {
    private String prop;

    PropertyChangeSupport pcs = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener l) {

    public void removePropertyChangeListener(PropertyChangeListener l) {

    public String getProp() {
        return prop;

    public void setProp(String prop) {
        pcs.firePropertyChanged("prop", this.prop, this.prop = prop);

Fortunately, with Groovy and the @Bindable annotation, this code can be greatly simplified:


class MyBean {
    @Bindable String prop

Now pair that with Groovy's Swing builder new bind() method, define a text field and bind its value to a property of your data model:

さて、テキストフィールドとそれに紐付けられた、あるデータモデルのプロパティの値が紐付けの宣言を行うGroovyのSwingビルダーにあるnew bind()メソッドを見てみましょう。

textField text: bind(source: myBeanInstance, sourceProperty: 'prop')

Or even:


textField text: bind { myBeanInstance.prop }

The binding also works with simple expressions in the closure, for instance something like this is possible too:


bean location: bind { pos.x + ', ' + pos.y }

You may also be interested in having a look at ObservableMap and ObservableList, for a similar mechanism on maps and lists.

また、マップとリストの似たような機能を持つ ObservableMap と ObservableList に興味を持っているかもしれません。

Along with @Bindable, there's also a @Vetoable transformation for when you need to be able to veto some property change. Let's consider a Trompetist class, where the performer's name is not allowed to contain the letter 'z':

@Bindable に加えて、@Vetoable もまた、プロパティを変更不可とするように変換できます。では、実行者の名前に'z'を含むことを許可しないという条件を持ったTrompetistクラスで考えてみましょう。

import java.beans.*
import groovy.beans.Vetoable

class Trumpetist {
    @Vetoable String name

def me = new Trumpetist()
me.vetoableChange = { PropertyChangeEvent pce ->
    if (pce.newValue.contains('z'))
        throw new PropertyVetoException("The letter 'z' is not allowed in a name", pce)
} = "Louis Armstrong"

try { = "Dizzy Gillespie"
    assert false: "You should not be able to set a name with letter 'z' in it."
} catch (PropertyVetoException pve) {
    assert true

Looking at a more thorough Swing builder example with binding:


import groovy.swing.SwingBuilder
import groovy.beans.Bindable
import static javax.swing.JFrame.EXIT_ON_CLOSE

class TextModel {
    @Bindable String text

def textModel = new TextModel() {
    frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
          locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
        gridLayout cols: 1, rows: 2
        textField id: 'textField'
        bean textModel, text: bind{ textField.text }
        label text: bind{ textModel.text }

Running this script shows up a frame with a text field and a lable below, and the label's text is bound on the text field's content.


SwingBuilder has evolved so nicely in the past year that the Groovy Swing team decided to launch a new project based on it, and on the Grails foundations: project Griffon was born. Griffon proposes to bring the Convention over Configuration paradigm of Grails, as well as all its project structure, plugin system, gant scripting capabilities, etc.

SwingBuilderはここ一年くらいでとてもよい進化を遂げて来たので、GroovyのSwingチームはこの機能をベースとした新たなプロジェクトを立ち上げることを決めました。Griffonは、Grailsのパラダイムである 設定より規約(CoC: Convention over Configuration) を採用して、それ自身のプロジェクト構造やプラグインシステム、Gantのスクリプティング能力などをうまくこなしています。

If you are developing Swing rich clients, make sure to have a look at Griffon.