Ronak Jain
Ronak Jain

Reputation: 2440

How to get row number in TableView at specific point?

I am creating TableView in JavaFX. I want to show Context Menu in right click of mouse. So I am doing as given below.

    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 I want to do that Context Menu should be only visible if I clicked on any rows in TableView. i.e. How would I get row number in TableView at specific point, So that my Context Menu should be only visible,if I clicked on any row of TableView.

Upvotes: 1

Views: 1045

Answers (1)

Alexander Kirov
Alexander Kirov

Reputation: 3654

I could suggest a bit different solution, if a row number is not obligatively needed.

Each node has a method Node.getChildrenUnmodifiable(), which returns the list of direct subnodes.

Having done a recursive search, using recursive calls of that method for nodes -> subnodes -> subsubnodes etc you can find an object of a class com.sun.javafx.scene.control.skin.VirtualFlow.

That is a Node, and Parent, which is responsible for cells rendering (that is something, which contains scrollBars and shows you cells - content of tableView).

You can call setOnMouseClick(...) and set a handler for it.

Upvotes: 1

Related Questions