|
3 | 3 | import java.util.function.BiConsumer;
|
4 | 4 | import java.util.function.Function;
|
5 | 5 |
|
| 6 | +import javafx.geometry.Bounds; |
| 7 | +import javafx.geometry.Point2D; |
6 | 8 | import javafx.scene.control.ContextMenu;
|
7 | 9 | import javafx.scene.control.TableRow;
|
8 | 10 | import javafx.scene.control.TableView;
|
9 | 11 | import javafx.scene.control.TreeTableCell;
|
10 | 12 | import javafx.scene.input.DragEvent;
|
| 13 | +import javafx.scene.input.KeyCode; |
| 14 | +import javafx.scene.input.KeyEvent; |
11 | 15 | import javafx.scene.input.MouseDragEvent;
|
12 | 16 | import javafx.scene.input.MouseEvent;
|
13 | 17 | import javafx.util.Callback;
|
@@ -101,6 +105,23 @@ public TableRow<S> call(TableView<S> tableView) {
|
101 | 105 | }
|
102 | 106 | event.consume();
|
103 | 107 | });
|
| 108 | + |
| 109 | + // Activate context menu if user presses the "context menu" key |
| 110 | + tableView.addEventHandler(KeyEvent.KEY_RELEASED, event -> { |
| 111 | + boolean rowFocused = !row.isEmpty() && tableView.getFocusModel().getFocusedIndex() == row.getIndex(); |
| 112 | + if (event.getCode() == KeyCode.CONTEXT_MENU && rowFocused) { |
| 113 | + // Get center of focused cell |
| 114 | + Bounds anchorBounds = row.getBoundsInParent(); |
| 115 | + double x = anchorBounds.getMinX() + anchorBounds.getWidth() / 2; |
| 116 | + double y = anchorBounds.getMinY() + anchorBounds.getHeight() / 2; |
| 117 | + Point2D screenPosition = row.getParent().localToScreen(x, y); |
| 118 | + |
| 119 | + if (row.getContextMenu() == null) { |
| 120 | + row.setContextMenu(contextMenuFactory.apply(row.getItem())); |
| 121 | + } |
| 122 | + row.getContextMenu().show(row, screenPosition.getX(), screenPosition.getY()); |
| 123 | + } |
| 124 | + }); |
104 | 125 | }
|
105 | 126 |
|
106 | 127 | if (toOnDragDetected != null) {
|
|
0 commit comments