Groovy的SwingBuilder绑定到多个属性 [英] Groovy SwingBuilder bind to multiple properties

查看:218
本文介绍了Groovy的SwingBuilder绑定到多个属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有绑定的属性使用SwingBuilder另一个对象的多个属性的方法吗?例如,我要一个按钮的enabled属性绑定到两个文本字段 - 该按钮时,才会启用时,这两个文本字段都是非空的

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.

推荐答案

您可以做这样的事情:

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

如你所见,结合2文本框在字段在我们的模型,然后绑定启用的按钮是真实的,如果这两个文本1 文本2 非空

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

这篇关于Groovy的SwingBuilder绑定到多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆