1

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

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

你能帮助我吗?看待。

4

2 回答 2

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

}

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

于 2017-11-13T15:22:47.573 回答
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 ;
        });
于 2017-11-15T11:01:06.683 回答