Reputation: 49
In my project I have a TreeTableView with 5 columns. I need a different context Menu for every columns. I have created a ContextMenu in Source Builder for every columns (in the example you see only one for "value" column), but the menù appears only with right click on column header, but I need the menù appears only on right click on cell value
<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>
I try to add this code for check the MouseClick event, but event is intercepted only on TreeTableView "valueTable" and not on 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");
});
Can you help me? Regard.
Upvotes: 1
Views: 904
Reputation: 3186
You can do it by implementing a custom TreeTableCell
then add the ContextMenu
to the cell instead of the column like:
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);
}
}
}
}
Of course you can set a different menu for the second column same as it is set for the first one.
Upvotes: 2
Reputation: 49
I have resolved with
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 ;
});
Upvotes: 0