我有一个非常简单的TreeView示例。
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableColumn.CellDataFeatures;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;
public class TreeTableViewSample extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage stage) {
stage.setTitle("Tree Table View Samples");
final Scene scene = new Scene(new Group(), 200, 400);
Group sceneRoot = (Group)scene.getRoot();
//Creating tree items
final TreeItem<String> childNode1 = new TreeItem<>("Child Node 1");
final TreeItem<String> childNode2 = new TreeItem<>("Child Node 2");
final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3");
//Creating the root element
final TreeItem<String> root = new TreeItem<>("Root node");
root.setExpanded(true);
//Adding tree items to the root
root.getChildren().setAll(childNode1, childNode2, childNode3);
//Creating a column
TreeTableColumn<String,String> column = new TreeTableColumn<>("Column");
column.setPrefWidth(150);
//Defining cell content
column.setCellValueFactory((CellDataFeatures<String, String> p) ->
new ReadOnlyStringWrapper(p.getValue().getValue()));
//Creating a tree table view
final TreeTableView<String> treeTableView = new TreeTableView<>(root);
treeTableView.getColumns().add(column);
treeTableView.setPrefWidth(152);
treeTableView.setShowRoot(true);
sceneRoot.getChildren().add(treeTableView);
stage.setScene(scene);
stage.show();
}
}
我对如何按名称对树节点进行排序感兴趣?
此功能是否已在JavaFX中实现,或者我需要实现自定义树单元格?
我有什么可以使用的例子吗?
答案 0 :(得分:3)
默认情况下,每个TableColumn
上的项目只需单击其标题(一次或两次)即可进行排序,以获得默认排序顺序(默认情况下为升序或降序)。
默认比较器为String.compareTo
,它按字典顺序比较两个字符串。
但你可以实现自己的。例如,这将按字符串的长度排序:
// compare by length of the strings
column.setComparator(Comparator.comparing(String::length));
这个将首先按长度排序,然后在长度相等的情况下按名称排序:
// compare by length first, and then lexicographically
column.setComparator(Comparator.comparing(String::length).thenComparing(String::compareTo));
编辑:由于示例引用了TreeTableView
,但是OP要求TreeView
,这就是项目的排序方式:
1)由于我们要添加一组项目,我们可以在将子项添加到根
之前对其进行排序@Override
public void start(Stage stage) {
stage.setTitle("Tree Table View Samples");
final Scene scene = new Scene(new Group(), 200, 400);
Group sceneRoot = (Group)scene.getRoot();
//Creating tree items
final TreeItem<String> childNode1 = new TreeItem<>("Child Node 10");
final TreeItem<String> childNode2 = new TreeItem<>("Child Node Two");
final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3");
//Creating the root element
final TreeItem<String> root = new TreeItem<>("Root node");
root.setExpanded(true);
List<TreeItem<String>> list = Arrays.asList(childNode1, childNode2, childNode3);
// sort by length of the item's names
list.sort(Comparator.comparing(t->t.getValue().length()));
//Adding tree items to the root
root.getChildren().setAll(list);
TreeView<String> tree = new TreeView<> (root);
sceneRoot.getChildren().add(tree);
stage.setScene(scene);
stage.show();
}
2)一旦我们将项目添加到根目录,我们就可以向根目录提供Comparator
:
@Override
public void start(Stage stage) {
stage.setTitle("Tree Table View Samples");
final Scene scene = new Scene(new Group(), 200, 400);
Group sceneRoot = (Group)scene.getRoot();
//Creating tree items
final TreeItem<String> childNode1 = new TreeItem<>("Child Node 10");
final TreeItem<String> childNode2 = new TreeItem<>("Child Node Two");
final TreeItem<String> childNode3 = new TreeItem<>("Child Node 3");
//Creating the root element
final TreeItem<String> root = new TreeItem<>("Root node");
root.setExpanded(true);
//Adding tree items to the root
root.getChildren().setAll(childNode1, childNode2, childNode3);
TreeView<String> tree = new TreeView<> (root);
// sort by length of the item's names
root.getChildren().sort(Comparator.comparing(t->t.getValue().length()));
sceneRoot.getChildren().add(tree);
stage.setScene(scene);
stage.show();
}