BeansBinding a JTable in NetBeans

匿名 (未验证) 提交于 2019-12-03 00:53:01

问题:

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:

  1. Right-Click the JTable.
  2. Click "Table Contents".
    1. Binding Source: Form
    2. Binding expression: ${var} (where var is the name of the list of beans).
  3. Click the "Columns" tab.
  4. 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); } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!