diff --git a/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java b/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java index 72d1de5e572..6e053fc3a40 100644 --- a/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java +++ b/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java @@ -3,11 +3,15 @@ import java.util.function.BiConsumer; import java.util.function.Function; +import javafx.geometry.Bounds; +import javafx.geometry.Point2D; import javafx.scene.control.ContextMenu; import javafx.scene.control.TableRow; import javafx.scene.control.TableView; import javafx.scene.control.TreeTableCell; import javafx.scene.input.DragEvent; +import javafx.scene.input.KeyCode; +import javafx.scene.input.KeyEvent; import javafx.scene.input.MouseDragEvent; import javafx.scene.input.MouseEvent; import javafx.util.Callback; @@ -101,6 +105,23 @@ public TableRow call(TableView tableView) { } event.consume(); }); + + // Activate context menu if user presses the "context menu" key + tableView.addEventHandler(KeyEvent.KEY_RELEASED, event -> { + boolean rowFocused = !row.isEmpty() && tableView.getFocusModel().getFocusedIndex() == row.getIndex(); + if (event.getCode() == KeyCode.CONTEXT_MENU && rowFocused) { + // Get center of focused cell + Bounds anchorBounds = row.getBoundsInParent(); + double x = anchorBounds.getMinX() + anchorBounds.getWidth() / 2; + double y = anchorBounds.getMinY() + anchorBounds.getHeight() / 2; + Point2D screenPosition = row.getParent().localToScreen(x, y); + + if (row.getContextMenu() == null) { + row.setContextMenu(contextMenuFactory.apply(row.getItem())); + } + row.getContextMenu().show(row, screenPosition.getX(), screenPosition.getY()); + } + }); } if (toOnDragDetected != null) {