Groovy 可绑定默认值?
我想要一个文本字段,其值始终反映给定对象中特定字段的值。我认为 Bindable 可能是做到这一点的方法。但是,使用以下示例:
#!/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"
修改自:
http://groovy.codehaus.org/ Bindable+and+Vetoable+transformation
仅将标签文本设置为textModel的文本,而不是textField的文本。
有什么想法吗???
谢谢 米莎
附注我似乎能够得到相反的行为,其中 TextField 反映变量的状态,但它的值不会更新,如果我这样做:
#!/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"
pps 如果我添加两者:
#!/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,文本:绑定{textField.text} 标签文本:bind{ textModel.text } } 我
textModel.text="AAAA"
在线程“AWT-EventQueue-0”java.lang.IllegalStateException 中遇到
异常:尝试在通知 p.pps 中进行变异
这是我最好的解决方案:
#!/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 } 标签文本:bind{ textModel.text } } }
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 }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Griffon 绑定指南,将
mutual
属性描述为您想要的。即使在本例中您没有使用 Griffon,bind
似乎也是一个标准的 Groovy 功能。如果您像这样创建textField
:textField
将从textModel.text
获取其初始值,当用户在字段中键入时向其写入更新,并在textModel.text
发生更改时显示更新的值(例如,来自某个后台线程)。当我尝试像这样绑定两个文本输入时,我开始收到您描述的 IllegalStateExceptions,但似乎一个输入和多个标签都可以。The Griffon guide on binding, describes the
mutual
property as being what you want. Even though you're not using Griffon in this case,bind
seems to be a standard Groovy feature. If you createtextField
like this:textField
will get its initial value fromtextModel.text
, write updates to it when the user types in the field, and display the updated value when changes totextModel.text
occur (from some background thread, say). When I tried to bind two text inputs like this, I started getting the IllegalStateExceptions you described, but it seems one input and multiple labels are fine.