不了解Netbeans,只知道它使用了一个版本的Beansbinding,所以下面肯定可以以某种方式应用
使用绑定框架的整个想法是,您永远不会直接与视图对话,而是完全专注于您的模型(或 bean):此类模型的某些属性绑定到视图的属性,并且您的代码只监听更改在你的bean的属性中。“SelectedElement”是绑定的人工属性(实际上是 JTableAdapterProvider,但这不是您需要知道的 :-),因此将您的模型属性绑定到该属性 - 这是手动执行此操作的片段:
// model/bean
public class AlbumManagerModel .. {
// properties
ObservableList<Album> albums;
Album selectedAlbum;
// vents the list of elements
ObservableList<Album> getManagedAlbums() {
return albums;
}
// getter/setter
public Album getSelectedAlbum() {
return selectedAlbum;
}
public void setSelectedAlbum(Album album) {
Album old = getSelectedAlbum();
this.selectedAlbum = album;
firePropertyChange("selectedAlbum", old, getSelectedAlbum());
}
}
// bind the manager to a JTable
BindingGroup context = new BindingGroup();
// bind list selected element and elements to albumManagerModel
JTableBinding tableBinding = SwingBindings.createJTableBinding(
UpdateStrategy.READ,
albumManagerModel.getManagedAlbums(), albumTable);
context.addBinding(tableBinding);
// bind selection
context.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
albumManagerModel, BeanProperty.create("selectedAlbum"),
albumTable, BeanProperty.create("selectedElement_IGNORE_ADJUSTING")
));
// bind columns
tableBinding.addColumnBinding(BeanProperty.create("artist"));
...
context.bind();