Reputation: 4209
I would like to pop up a node relative to a selected row in a TableView. The UI that we are following uses an Edit Button to open the pop over, and I would like to place it either under the row or above the row depending on where the row is in the TableView.
I can't seem to find a way to locate the X & Y coordinates of the row without clicking on the row.
Since my application needs to be fully keyboard navigable, I need to be able to determine that location from a selected row, even if it was selected via the keyboard.
I.e. something like this
myTableView.getSelectionModel().getSelectedItem().location() or something like that.
I have looked at a couple of examples here, but none seems to provide enough detail on how to get the coordinates based on the click somewhere else on the screen.
Thanks!
Upvotes: 1
Views: 1826
Reputation: 209340
You can do something along these lines:
TableView<T> table = ... ;
ObjectProperty<TableRow<T>> lastSelectedRow = new SimpleObjectProperty<>();
table.setRowFactory(tableView -> {
TableRow<T> row = new TableRow<T>();
row.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
lastSelectedRow.set(row);
}
}
return row ;
});
Then at any time lastSelectedRow.get()
will return the actual node representing the last selected row, or null
if no row has been selected (under the current implementation of TableView
's behavior, I don't think this will happen). You can then use the various getBoundsIn*()
methods, perhaps combined with localTo*()
methods, to get the location of the row when you need it: for example
TableRow<?> row = lastSelectedRow.get();
Bounds bounds = row.localToScreen(row.getBoundsInLocal());
will give the bounds of the last selected row relative to the screen coordinates.
Strange things may happen if the user selects something with the mouse, then scrolls the selected row out of view; but then you need to figure out how to deal with that however you get the coordinates.
Upvotes: 2