1

TableView在 JavaFX 中创建。我想用Context Menu鼠标右键显示。所以我正在做如下给出的。

    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);

但我想这样做Context Menu应该只有在我点击任何行时才可见TableView。即我将如何在特定点获得 TableView 中的行号,这样我Context Menu应该只可见,如果我点击任何行TableView

4

1 回答 1

1

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.

于 2013-01-05T23:12:24.383 回答