I am creating a TableView in JavaFX. In which I want to show the Context Menu right mouse button in the tableView. Therefore, I add an EventHandler to the table as shown below:
TableView tableView=new TableView(); EventHandler event = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent me) { if (me.getButton() == MouseButton.SECONDARY) { tableView.getContextMenu().show(tableView, me.getSceneX(), me.getSceneY()); } } }; tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, event);
But my problem is that the Context Menu displayed wherever I right-click on any part of the table.
I want to do this so that the Context Menu displayed only if I clicked on any rows in the TableView .
i.e. How can I get the row number in the TableView at a specific point, so that my Context Menu should only be visible if I clicked on any row in the TableView .
source share