Reputation: 757
How to detect, which column(name or id whatever) was selected when user clicked on certain cell?
Upvotes: 2
Views: 7078
Reputation: 1
I've been trying to apply this type of processing, albeit in Kotlin. I am pretty sure the programming language (Java vs Kotlin) should not matter in this situation.
Unfortunately, the approach suggested above did not work for me (perhas I made a mistake) as I received an error message " 2 type arguments expected for class TablePosition<S : Any!, T : Any!>"
Here is the code I tried:
val listChangeListener = ListChangeListener<TablePosition>()
{ fun onChanged(c: ListChangeListener<TablePosition>)
{ println("Selection changed: ")
}
}
this.getSelectionModel().setCellSelectionEnabled(true);
var selectedCells = selectionModel.getSelectedCells();
selectedCells.addListener(listChangeListener)
I have found references elsewhere to this problem - Why is a JavaFXML TablePosition instance considered as a raw type when type arguments are declared?
I did find a solution. I added a mouse click event handler for the tableview, and in the event handler, when it is triggered, I then get the selected indices using
tableView.getSelectionModel().getSelectedCells()
PhilTroy
Upvotes: 0
Reputation: 209340
To enable individual cells to be selected, instead of entire rows, call
tableView.getSelectionModel().setCellSelectionEnabled(true);
To keep track of which cell(s) are selected, you can do
final ObservableList<TablePosition> selectedCells = table.getSelectionModel().getSelectedCells();
selectedCells.addListener(new ListChangeListener<TablePosition>() {
@Override
public void onChanged(Change change) {
for (TablePosition pos : selectedCells) {
System.out.println("Cell selected in row "+pos.getRow()+" and column "+pos.getTableColumn().getText());
}
});
});
Upvotes: 5