Sorting TreeView by name

I have this very simple TreeView example.

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

      

I am wondering how can I sort the tree nodes by name?

Is this functionality already implemented in JavaFX or do I need to implement a custom tree cell?

Is there any example I can use?

+3


source to share


1 answer


By default, the items in each TableColumn

can be sorted by simply clicking on its title once or twice to get the default sort order (default ascending or descending order).

The default comparator String.compareTo

that compares two strings lexicographically.

But you can implement your own. For example, this will sort by line length:

// compare by length of the strings
column.setComparator(Comparator.comparing(String::length));

      

And this one will be sorted first by length, then in case of equal length, by name:

// compare by length first, and then lexicographically
column.setComparator(Comparator.comparing(String::length).thenComparing(String::compareTo));

      



EDIT : As the example relates to TreeTableView

, but the OP asks TreeView

, this is how the elements can be sorted:

1) Since we are adding a collection of items, we can sort it before adding children to the root

@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 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) Once we have added the items to the root directory, we can provide the Comparator

root:

@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 names          
    root.getChildren().sort(Comparator.comparing(t->t.getValue().length()));

    sceneRoot.getChildren().add(tree);
    stage.setScene(scene);
    stage.show();        
}

      

+3


source







All Articles