有没有一种方法可以使用groovy swing构建器绑定语法将数据绑定到列表和/或表?我只能找到将字符串和数字等简单属性绑定到文本字段、标签或按钮文本的简单示例。
发布于 2010-02-19 21:37:53
环顾四周,我能看到的最好的结果是使用GlazedLists而不是标准的Swing列表
http://www.jroller.com/aalmiray/entry/glazedlists_groovy_not_your_regular
发布于 2011-02-16 08:57:35
有一个GlazedList plugin。this article是非常有用的。格里芬的人对GlazedLists发誓。
发布于 2014-10-29 01:25:09
我只是做了这样的事情--这真的不是很难手动完成。这仍然是一项正在进行的工作,但如果它对任何人有帮助,我可以尽我所能。到目前为止,它在两个方向上绑定了数据(更新数据更新组件,编辑表更新数据,并向“行”的任何propertyChangeListeners发送通知)
我使用一个类来定义表的一行。您可以创建此类来定义表的性质。它看起来像这样:
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();
}
}
}可能有一种方法可以在不添加/删除的情况下通过公开可绑定列表来做到这一点,但这似乎是更多的工作,而没有太多的好处。
在某种程度上,我可能会将完成的类放在某个地方--如果您已经读到这里并且仍然感兴趣,请在评论中回复,我一定会尽快完成它。
https://stackoverflow.com/questions/2286211
复制相似问题