groovy swingbuilder 可绑定列表和表格

发布于 2024-08-22 04:05:33 字数 86 浏览 8 评论 0原文

有没有办法使用 groovy swing 构建器绑定语法将数据绑定到列表和/或表?我只能找到将字符串和数字等简单属性绑定到文本字段、标签或按钮文本的简单示例。

Is there a way to bind data to a list and/or a table using the groovy swing builder bind syntax? I could only find simple examples that bind simple properties like strings and numbers to a text field, label, or button text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

·深蓝 2024-08-29 04:05:33

环顾四周,我能看到的最好的结果是使用 GlazedLists 而不是标准 Swing 列表

http:// /www.jroller.com/aalmiray/entry/glazedlists_groovy_not_your_regular

Had a look round, and the best I could see was using GlazedLists rather than standard Swing lists

http://www.jroller.com/aalmiray/entry/glazedlists_groovy_not_your_regular

扮仙女 2024-08-29 04:05:33

有一个 GlazedList 插件这篇文章非常有帮助。狮鹫们对 GlazedLists 非常信赖。

There is a GlazedList plugin. And this article is very helpful. The Griffon guys swear by GlazedLists.

走走停停 2024-08-29 04:05:33

我只是做了这样的事情——手动做起来确实不难。这仍然是一项正在进行的工作,但如果它对任何人有帮助,我可以提供我所拥有的。到目前为止,它在两个方向上绑定数据(更新数据更新组件,编辑表更新数据并向“行”的任何 propertyChangeListener 发送通知)

我使用一个类来定义表的一行。您创建此类来定义表的性质。它看起来像这样:

class Row
{
    // allows the table to listen for changes and user code to see when the table is edited
    @Bindable
    // The name at the top of the column
    @PropName("Col 1")
    String c1
    @Bindable
    // In the annotation I set the default editable to "false", here I'll make c2 editable.
    // This annotation can (and should) be expanded to define more column properties.
    @PropName(value="Col 2", editable=true)
    String c2
}

请注意,一旦将其余代码打包到一个类中,这个“Row”类就是创建新表时唯一需要创建的东西。您为每一行创建此类的实例,将它们添加到表中,然后就完全完成了——除了将表放入框架之外,没有其他 gui 工作。

这个类可以包含更多的代码——我打算让它包含一个轮询数据库并更新绑定属性的线程,然后表应该立即获取更改。

为了提供自定义列属性,我定义了一个如下所示的注释(我计划添加更多):

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface PropName {
    String value();
    boolean editable() default false
}

其余的是构建表的类。我将它保存在一个单独的类中,以便可以重用它(通过使用不同的“Row”类实例化它)

注意:我在粘贴它时将其删除,因此如果不做一点工作它可能无法运行(可能是大括号)。它曾经包含一个框架,我将其删除以仅包含桌子。您需要将从 getTable() 返回的表包装在框架中。

public class AutoTable<T>
{   
    SwingBuilder builder // This way external code can access the table
    def values=[] // holds the "Row" objects
    PropertyChangeListener listener={repaint()} as PropertyChangeListener

    def AutoTable(Class<T> clazz)
    {
      builder = new SwingBuilder()
      builder.build{
        table(id:'table') {
          tableModel(id:'tableModel') {
            clazz.declaredFields.findAll{ 
              it.declaredAnnotations*.annotationType().contains(PropName.class)}.each {
                def annotation=it.declaredAnnotations.find{it.annotationType()==PropName.class 
              }
              String n=annotation.value()
              propertyColumn(header:n, propertyName:it.name, editable:annotation.editable())
            }
          }
          tableModel.rowsModel.value=values
        }
    }
    // Use this to get the table so it can be inserted into a container
    def getTable() {
        return builder.table
    }

    def add(T o) {
        values.add(o)
        o.addPropertyChangeListener(listener)
    }

    def remove(T o) {
        o.removePropertyChangeListener(listener)
        values.remove(o)
    }

    def repaint() {
        builder.doLater{
            builder.table.repaint();
        }
    }   
}

可能有一种方法可以通过公开可绑定列表来做到这一点,而无需添加/删除,但这似乎需要更多工作,但没有太多好处。

在某个时候,我可能会把完成的课程放在某个地方——如果您已经读到这里并且仍然感兴趣,请回复评论,我一定会尽快这样做。

I just did something like this--it's really not that hard to do manually. It's still a work in progress, but if it helps anyone I can give what I have. So far it binds the data in both directions (Updating the data updates the component, editing the table updates the data and sends a notification to any propertyChangeListeners of the "Row")

I used a class to define one row of a table. You create this class to define the nature of your table. It looks something like this:

class Row
{
    // allows the table to listen for changes and user code to see when the table is edited
    @Bindable
    // The name at the top of the column
    @PropName("Col 1")
    String c1
    @Bindable
    // In the annotation I set the default editable to "false", here I'll make c2 editable.
    // This annotation can (and should) be expanded to define more column properties.
    @PropName(value="Col 2", editable=true)
    String c2
}

Note that once the rest of the code is packaged up in a class, this "Row" class is the ONLY thing that needs to be created to create a new table. You create instances of this class for each row, add them to the table and you are completely done--no other gui work aside from getting the table into a frame.

This class could include quite a bit more code--I intend to have it contain a thread that polls a database and updates the bound properties, then the table should pick up the changes instantly.

In order to provide custom column properties I defined an annotation that looks like this (I plan to add more):

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface PropName {
    String value();
    boolean editable() default false
}

The rest is a class that builds the table. I keep it in a separate class so it can be reused (by instantiating it with a different "Row" class)

NOTE: I butchered this as I pasted it in so it may not run without a little work (braces probably). It used to include a frame which I removed to just include the table. You need to wrap the table returned from getTable() in a frame..

public class AutoTable<T>
{   
    SwingBuilder builder // This way external code can access the table
    def values=[] // holds the "Row" objects
    PropertyChangeListener listener={repaint()} as PropertyChangeListener

    def AutoTable(Class<T> clazz)
    {
      builder = new SwingBuilder()
      builder.build{
        table(id:'table') {
          tableModel(id:'tableModel') {
            clazz.declaredFields.findAll{ 
              it.declaredAnnotations*.annotationType().contains(PropName.class)}.each {
                def annotation=it.declaredAnnotations.find{it.annotationType()==PropName.class 
              }
              String n=annotation.value()
              propertyColumn(header:n, propertyName:it.name, editable:annotation.editable())
            }
          }
          tableModel.rowsModel.value=values
        }
    }
    // Use this to get the table so it can be inserted into a container
    def getTable() {
        return builder.table
    }

    def add(T o) {
        values.add(o)
        o.addPropertyChangeListener(listener)
    }

    def remove(T o) {
        o.removePropertyChangeListener(listener)
        values.remove(o)
    }

    def repaint() {
        builder.doLater{
            builder.table.repaint();
        }
    }   
}

There is probably a way to do this without the add/remove by exposing a bindable list but it seemed like more work without a lot of benifit.

At some point I'll probably put the finished class up somewhere--if you have read this far and are still interested, reply in a comment and I'll be sure to do it sooner rather than later.

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