views:

30

answers:

0

Dear All:

I would like to have a text field whose value always reflects that of a certain field in a given object. I thought Bindable might be the way to do this. However, using the following example:

#!/usr/bin/env groovy

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()

def builder=new SwingBuilder()
builder.build {
    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 }
    }
}

textModel.text="AAAA"

modified from:

http://groovy.codehaus.org/Bindable+and+Vetoable+transformation

only the label text is set to that of textModel, but not that of the textField.

Any ideas???

Thank you Misha

p.s. I seem to be able to get the opposite behavior, where the TextField reflects that state of the variable, but its value is not updated, if I do:

#!/usr/bin/env groovy

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()

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

textModel.text="AAAA"

p.p.s. If I add both:

#!/usr/bin/env groovy

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()

def builder=new SwingBuilder()
builder.build {
    frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
          locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
        gridLayout cols: 1, rows: 2

      textField id: 'textField',text:bind{ textModel.text }
  bean textModel, text: bind{ textField.text }
        label text: bind{ textModel.text }
    }
}

textModel.text="AAAA"

I get

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification

p.p.p.s. This is my best solution:

#!/usr/bin/env groovy

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()
textModel.text="AAAA"

def builder=new SwingBuilder()
builder.build {
    frame( title: 'Binding Example (Groovy)', size: [240,100], show: true,
          locationRelativeTo: null, defaultCloseOperation: EXIT_ON_CLOSE ) {
        gridLayout cols: 1, rows: 2

      textField id: 'textField',text:textModel.text
  bean textModel, text: bind{ textField.text }
  label text: bind{ textModel.text }
    }
}