Groovy 可绑定默认值?

发布于 2024-09-05 22:16:06 字数 3115 浏览 5 评论 0原文

我想要一个文本字段,其值始终反映给定对象中特定字段的值。我认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱情眠于流年 2024-09-12 22:16:06

Griffon 绑定指南,将 mutual 属性描述为您想要的。即使在本例中您没有使用 Griffon,bind 似乎也是一个标准的 Groovy 功能​​。如果您像这样创建 textField

  textField id: 'textField', text: bind('text', source: textModel, mutual: true)

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 create textField like this:

  textField id: 'textField', text: bind('text', source: textModel, mutual: true)

textField will get its initial value from textModel.text, write updates to it when the user types in the field, and display the updated value when changes to textModel.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文