java Beans 在 NetBeans 中绑定 JTable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1162166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BeansBinding a JTable in NetBeans
提问by User1
I want map a List of beans to a JTable. The idea is that each column will be a preselected field in the bean and each row will be a bean in the List. Slide #32 here looks very promising: http://swinglabs.org/docs/presentations/2007/DesktopMatters/beans-binding-talk.pdf
我想将 bean 列表映射到 JTable。这个想法是每一列都将是 bean 中的一个预选字段,而每一行将是 List 中的一个 bean。这里的幻灯片 #32 看起来非常有前途:http: //swinglabs.org/docs/presentations/2007/DesktopMatters/beans-binding-talk.pdf
However, NetBeans is not very friendly in letting me assign a bean field to a column. I can right-click the JTable and click Bind->Elements and bind it to my List of beans. However, it will not let me specify what goes in each column. The only option is to create the binding myself which pretty much makes NetBeans useless for this type of thing.
但是,NetBeans 在让我将 bean 字段分配给列方面并不是很友好。我可以右键单击 JTable 并单击 Bind->Elements 并将其绑定到我的 bean 列表。但是,它不会让我指定每列中的内容。唯一的选择是自己创建绑定,这几乎使 NetBeans 无法用于此类事情。
Is there a detail I'm missing? It appears that JTable BeansBinding in NetBeans is just broken.
有没有我遗漏的细节?看来 NetBeans 中的 JTable BeansBinding 刚刚损坏。
Thanks
谢谢
回答by User1
I have it working. You can't really use the "Bind" menu option for JTables. Here's how to get it to work:
我有它的工作。您不能真正使用 JTables 的“绑定”菜单选项。以下是让它工作的方法:
- Right-Click the JTable.
- Click "Table Contents".
- Binding Source: Form
- Binding expression: ${var} (where var is the name of the list of beans).
- Click the "Columns" tab.
- Map the column to the expression. It should look something like ${id} not ${var.id}.
- 右键单击 JTable。
- 单击“表格内容”。
- 绑定来源:表格
- 绑定表达式:${var}(其中 var 是 bean 列表的名称)。
- 单击“列”选项卡。
- 将列映射到表达式。它应该看起来像 ${id} 而不是 ${var.id}。
Note: Each field mapped to a column must have a getter.
注意:映射到列的每个字段都必须有一个 getter。
回答by Kevin Day
As appealing as it may be to use the IDE for this sort of stuff, there's really no substitute for just coding it yourself.
尽管将 IDE 用于此类事情可能很有吸引力,但实际上没有什么可以替代自己编写代码。
Personally, I prefer Glazed Listsfor presenting beans in tables. Take the 2 minutes and watch the video, and I guarantee that you'll be hooked. With less than 15 lines of code, you'll get what you are looking for, and have a huge amount of control over the display - plus filtering, sorting and all sorts of other cool stuff when you are ready for it.
就个人而言,我更喜欢使用Glazed Lists在表格中展示 bean。花 2 分钟观看视频,我保证你会被迷住。只需不到 15 行代码,您就可以获得所需的内容,并对显示进行大量控制——此外,当您准备好时,还可以进行过滤、排序和各种其他很酷的事情。
回答by michaelr
Try making the list an observable one. change its initialization to something like this:
尝试使列表成为可观察的列表。将其初始化更改为如下所示:
list1 = ObservableCollections.observableList(new ArrayList<Person>());
Then a lot of staff should start working. If you are binding to a bean then make sure you fire a property changed event in the set method of the property you want bound add this code
然后很多员工应该开始工作。如果要绑定到 bean,请确保在要绑定的属性的 set 方法中触发属性更改事件添加此代码
private final PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.removePropertyChangeListener(listener);
}
fix imports and then do something like this
修复导入,然后做这样的事情
public void setFirstName(String firstName) {
String oldFirstName = this.firstName;
this.firstName = firstName;
changeSupport.firePropertyChange("firstName", oldFirstName, firstName);
}

