Reputation: 1953
I would like to make the 'folder' nodes in my JavaFX TreeView
expandable and collapsible but not selectable.
I found this discussion and looked into EventFilter
, but there does not appear to be any EventType
that corresponds with TreeView
selection changes. The second suggestion, a custom selection model, sounds like a deep dive to me. So, am I stuck allowing the selection events to trigger my listener and then sort through the trash there?
Upvotes: 5
Views: 2286
Reputation: 2569
You can override updateItem method in a TreeCell object . This is a single class app . So, you can test it . Be careful , this aproach would be work with leaf nodes only , because disabled parent nodes can't expand or collapse .
package com.app;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
TreeView<String> treeView = new TreeView<>();
treeView.setRoot(new TreeItem<>("People"));
treeView.getRoot().getChildren().addAll(
new TreeItem<>("Fernanado"),
new TreeItem<>("María"),
new TreeItem<>("Flor"),
new TreeItem<>("Pablo"),
new TreeItem<>("Francisca"));
treeView.setCellFactory(p -> new TreeCell<String>() {
@Override
public void updateItem(String string, boolean empty) {
super.updateItem(string, empty);
if (empty) {
setText("");
setGraphic(null);
} else {
setText(string);
setGraphic(getTreeItem().getGraphic());
// this will disable a TreeItem if String has "F" character
setDisable(string.contains("F"));
}
}
});
Scene scene = new Scene(new StackPane(treeView), 640, 480);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Upvotes: 0
Reputation: 455
There is a similar question posted here:
TreeView - Certain TreeItems are not allowed to be selected
If you use the FilteredTreeViewSelectionModel
in the answer that I posted there you can get the desired behaviour by creating a custom TreeItemSelectionFilter
and wiring everything up as follows:
MultipleSelectionModel<TreeItem<Object>> selectionModel = tree.getSelectionModel();
TreeItemSelectionFilter<Object> filter = TreeItem::isLeaf;
FilteredTreeViewSelectionModel<Object> filteredSelectionModel = new
FilteredTreeViewSelectionModel<>(tree, selectionModel, filter);
tree.setSelectionModel(filteredSelectionModel);
This will ensure that only leaf-nodes can be selected as you require.
Upvotes: 0
Reputation: 1851
It's a bit hacky, but I ended up doing it like this:
table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null && !newValue.isLeaf()) {
Platform.runLater(() -> table.getSelectionModel().clearSelection());
}
});
For me it was enough to just clear the selection when clicking a non-leaf node. However, it shouldn't be to hard to just reselect the oldValue
parameter, but be aware that this will fire a change event again (so does the clearSelection
call, that's why the newValue != null
check is necessary).
Upvotes: 4