Context Menu on a row of TableView?

后端 未结 3 1801
北荒
北荒 2021-02-05 18:46

I am using JavaFX and my application has a table and I can add elements to the table but I want to create a context menu that displays on a row when I right click on that row.

3条回答
  •  盖世英雄少女心
    2021-02-05 19:00

    try this..

    ContextMenu cm = new ContextMenu();
    MenuItem mi1 = new MenuItem("Menu 1");
    cm.getItems().add(mi1);
    MenuItem mi2 = new MenuItem("Menu 2");
    cm.getItems().add(mi2);
    
    table.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() {
    
        @Override
        public void handle(MouseEvent t) {
            if(t.getButton() == MouseButton.SECONDARY) {
                cm.show(table, t.getScreenX(), t.getScreenY());
            }
        }
    });
    

提交回复
热议问题