I have JTable which "elements" property binded to List of objects, this is master table. There is also details table, which "elements" property binded to selectedElement in master table. I did it with help of NetBeans GUI builder. Now I try to get something like this:
SomeEntityType selectedObject= (SomeEntityType) masterTable.getSelectedElement ()
in source code, but there is no such property in JTable, only "getSelectedRow". So, how can I get selected object from JTable binded to source (List of objects)? I have read similar questions, but find only link on getValueAt(rowId,columnId) method, but in my task it doesn't matter which column is selected, because full row is selected.
don't know about Netbeans, just know that it uses a version of Beansbinding, so the following certainly can applied somehow
The whole idea of using a binding framework is that you never directly talk to the view, but fully concentrate on your model (or bean): some property of such a model is bound to a property of a view and your code only listens to changes in the the properties of your bean. "SelectedElement" is an artificial property of the binding (actually, of the JTableAdapterProvider, but that's nothing you need to know :-), so bind your model property to that - here's a snippet of doing so manually:
// 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();