views:

1944

answers:

3

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

+2  A: 

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.

Kevin Day
It's funny that you mention that technology. I was seriously considering using it. I was a bit concerned about adding another dependency to our project, so I was hopeful beansbinding would be all we need. Have you noticed a benefit of Glazed Lists over beansbinding?
User1
oh yeah - Glazed Lists is far superior for list binding/handling than BB (I love BB, but only for regular property binding). GL is well worth the dependency (and it's a fantastic library). The support for lock semantics and automatic EDT dispatching is enough reason to use GL - but then you add on all the other fantastic things you can do (filtering, dynamic list transforms), and it's a very easy choice to make.
Kevin Day
since Beans Binding (JSR-295) is now dead in favor of Sun's work on JavaFX, it's probably better to try avoid using it as support for it will be somewhat limited. Another reason for using Glazed Lists.
Robin
Robin - JSR295 has been forked and is being developed as a true open source project called Better Beans Binding (http://kenai.com/projects/betterbeansbinding/pages/Home). JavaFX is not a direction that most of us are going to be heading (not soon, anyway), so a true binding framework for Java is still very much needed. But I do agree that GL is a better option for working with lists of data (there are a lot of problems with the observable collections in JSR295)
Kevin Day
What are the problems with BeansBinding?
User1
Not problems with BeansBinding, per se - but problems with how BeansBinding handles list binding. Long and short: the BB approach to list binding is very simplistic, and doesn't cover a huge number of important use cases (e.g. property changes that originate off the EDT). The GL approach is far better.
Kevin Day
I should also mention that it's quite possible (and highly desirable) to use BB (or BBB) for regular property binding, and GL for list binding. It's possible to use BB to bind a source list to a destination list, so you get the best of both worlds.
Kevin Day
+2  A: 

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.

User1
A: 

Try making the list an observable one. change its initialization to something like this:

list1 = ObservableCollections.observableList(new ArrayList());

Then alot 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); }

michaelr