1

Is there a way to bind a properties to multiple properties of another object using the SwingBuilder? For example, I want to bind a button's enabled property to two text fields - the button is only enabled when both text fields are non empty.

cdeszaq
  • 30,869
  • 25
  • 117
  • 173
Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

1 Answers1

2

You can do this sort of thing:

import groovy.beans.Bindable
import groovy.swing.SwingBuilder
import javax.swing.WindowConstants as WC

class CombinedModel {
  @Bindable String text1
  @Bindable String text2
}

def model = new CombinedModel()

SwingBuilder.build() {
    frame(title:'Multiple Bind Test', pack:true, visible: true, defaultCloseOperation:WC.EXIT_ON_CLOSE ) {
        gridLayout(cols: 2, rows: 0)

        label 'Input text 1: '
        textField( columns:10, id:'fielda' )

        label 'Input text 2: '
        textField( columns:10, id:'fieldb' )

        // Bind our two textFields to our model
        bean( model, text1: bind{ fielda.text } )
        bean( model, text2: bind{ fieldb.text } )

        label 'Button: '
        button( text:'Button', enabled: bind { model.text1 && model.text2 } )
    }
}

As you can see, that binds two textfields to fields in our model, then binds enabled for the button to be true if both text1 and text2 are non-empty

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • +1 for answer! but where do you finding these kind of Stuffs in Groovy? Any blogs? – Ant's Jun 15 '11 at 03:05
  • @ant I found some code on [Andres Almiray's blog](http://jroller.com/aalmiray/entry/swingbuilder_s_binding_revisited) (the creator of Griffon) here which helped me work out how to do this :-) – tim_yates Jun 15 '11 at 08:01