javafx - 按名称对 TreeView 排序

标签 javafx javafx-2 javafx-8

我有一个非常简单的 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 中实现,或者我需要实现自定义树单元?

有什么我可以使用的例子吗?

最佳答案

默认情况下,每个 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();        
}

关于javafx - 按名称对 TreeView 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960208/

相关文章:

Java 从另一个类更新 TableView 单元格值

java - javafx media可以播放mp4格式的视频文件吗?

JavaFX:鼠标落后于绘图

JavaFX TableView : Rapid change in items list not reflected

javafx-2 - 布局更改时的动画

java - 用于制作代码编辑器的 JSyntaxPane 的 JavaFX 等价物是什么?

javafx - 使用父类(super class)扩展 javafx.application.Application。为什么子类中的 Override 会被忽略?

animation - JavaFX Animation 类的 'rate' 属性如何表现?

java - 从 JavaFX 打印时设置要使用的 CSS

JavaFX KeyEvent 传播顺序