2014-09-21 113 views
3

我有这个非常简单的TreeView示例。按名称排序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中实现或者我需要实现自定义树单元格?

有没有我可以用的例子?

回答

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

你能告诉我一些例子为TreeView控件? – user1285928 2014-09-21 21:24:50

+0

非常感谢。 – chris 2015-04-27 19:16:40