可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
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.
Is there a detail I'm missing? It appears that JTable BeansBinding in NetBeans is just broken.
Thanks
回答1:
I have it working. You can't really use the "Bind" menu option for JTables. Here's how to get it to work:
- 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}.
Note: Each field mapped to a column must have a getter.
回答2:
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.
Personally, I prefer Glazed Lists for 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.
回答3:
Try making the list an observable one. change its initialization to something like this:
list1 = ObservableCollections.observableList(new ArrayList());
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
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); }