JavaFX TreeTableView和ContextMenu

时间:2017-11-13 14:11:58

标签: javafx contextmenu fxml treetableview

在我的项目中,我有一个包含5列的TreeTableView。 我需要为每列提供不同的上下文菜单。 我在Source Builder中为每个列创建了一个ContextMenu(在示例中,您只看到一个用于“value”列),但menù只出现右键单击列标题,但我需要menù仅在右键单击单元格时出现值

      <TreeTableView fx:id="valueTable" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER">
    <columns>
      <TreeTableColumn fx:id="context" prefWidth="483.20001524686813" sortable="false" text="Context" />
      <TreeTableColumn fx:id="tag" minWidth="50.0" prefWidth="90.0" sortable="false" text="Tag" />
      <TreeTableColumn fx:id="offset" minWidth="50.0" prefWidth="90.0" sortable="false" text="OffSet" />
      <TreeTableColumn fx:id="lenght" minWidth="50.0" prefWidth="90.0" sortable="false" text="Lenght" />
      <TreeTableColumn fx:id="value" prefWidth="367.99993896484375" sortable="false" text="Value">
           <contextMenu>
              <ContextMenu fx:id="contextMenuValue" >
                <items>
                  <MenuItem mnemonicParsing="false" text="Action 1" />
                  <MenuItem mnemonicParsing="false" text="Action 2" />
                  <MenuItem mnemonicParsing="false" text="Action 3" />
                  <MenuItem mnemonicParsing="false" text="Action 4" />
                </items>
              </ContextMenu>
           </contextMenu>
      </TreeTableColumn>
    </columns>
  </TreeTableView>

我尝试添加此代码以检查MouseClick事件,但事件仅在TreeTableView“valueTable”上截获,而不是在TreeTableColumn“value”上截获。

        value.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
                System.out.println("Work Cell");
        });
        valueTable.addEventHandler(MouseEvent.MOUSE_CLICKED, (MouseEvent e) -> {
            System.out.println("Work Table");
        });
你能帮帮我吗? 方面。

2 个答案:

答案 0 :(得分:2)

您可以通过实施自定义TreeTableCell然后将ContextMenu添加到单元格而不是列中来执行此操作:

public class Controller implements Initializable {

    @FXML
    private TreeTableView<MyModel> table;
    @FXML
    private TreeTableColumn<MyModel, String> first;
    @FXML
    private TreeTableColumn<MyModel, Boolean> second;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        first.setCellValueFactory(data -> data.getValue().getValue().nameProperty());
        first.setCellFactory(cell -> new MyCell());
        second.setCellValueFactory(data -> data.getValue().getValue().selectedProperty());

        MyModel john = new MyModel("John");
        MyModel andrew = new MyModel("Andrew");

        table.setRoot(new TreeItem<>());
        table.setShowRoot(false);
        table.getRoot().getChildren().add(new TreeItem<>(john));
        table.getRoot().getChildren().add(new TreeItem<>(andrew));

    }

    private class MyModel {

        private StringProperty name;
        private BooleanProperty selected;

        MyModel(String name) {
            this.name = new SimpleStringProperty(name);
            this.selected = new SimpleBooleanProperty(false);
        }

        StringProperty nameProperty() {
            return name;
        }

        BooleanProperty selectedProperty() {
            return selected;
        }
    }

    private class MyMenu extends ContextMenu {
        MyMenu() {
            getItems().add(new MenuItem("Test"));
            getItems().add(new MenuItem("Item"));

        }
    }

    private class MyCell extends TreeTableCell<MyModel, String> {
        MyCell() {
            // Here you can set the same menu for each cell. Then the column is having the same cell for every row
            setContextMenu(new MyMenu());
        }

        // Overridden just to show the text of the cell.
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
            } else {
                contextMenuProperty().bind(Bindings
                    // here you can define your own condition
                    .when(getTreeTableRow().getTreeItem().getValue().selectedProperty())
                    .then(new MyMenu())
                    .otherwise((MyMenu) null));
                setText(item);
            }
        }
    }

}

当然,您可以为第二列设置与第一列相同的菜单。

答案 1 :(得分:0)

我已经解决了

            value.setCellFactory(tc -> {
            TreeTableCell<MyModel, String> cell = new TreeTableCell<MyModel, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty) ;
                    setText(empty ? null : item);
                }
            };
            cell.setOnMouseClicked(e -> {
                if (! cell.isEmpty()) {
                    String userId = cell.getItem();
                    System.out.println("Work!");
                }
            });
            return cell ;
        });