Context Menu on a row of TableView?

后端 未结 3 1804
北荒
北荒 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 18:51

    JavaFX 8 (with Lambda):

    MenuItem mi1 = new MenuItem("Menu item 1");
    mi1.setOnAction((ActionEvent event) -> {
        System.out.println("Menu item 1");
        Object item = table.getSelectionModel().getSelectedItem();
        System.out.println("Selected item: " + item);
    });
    
    ContextMenu menu = new ContextMenu();
    menu.getItems().add(mi1);
    table.setContextMenu(menu);
    

    See also: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ContextMenu.html

提交回复
热议问题