不知道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();
我找到了簡單的解決方案: 1)在表單 上添加屬性「selectedElement」2)使用GUI構建器將JTable的「selectedElement」屬性綁定到此屬性 –