有没有办法使用SwingBuilder将一个属性绑定到另一个对象的多个属性?例如,我希望将一个按钮的enabled属性绑定到两个文本字段-只有当这两个文本字段都不为空时,该按钮才会被启用。
发布于 2011-06-15 04:54:16
你可以做这样的事情:
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 } )
}
}如您所见,它将两个文本字段绑定到我们模型中的字段,然后在text1和text2都为非空的情况下绑定enabled以使按钮为真
https://stackoverflow.com/questions/6349107
复制相似问题