首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX - TreeTableView sortPolicyProperty()

JavaFX - TreeTableView sortPolicyProperty()
EN

Stack Overflow用户
提问于 2016-06-08 08:22:31
回答 1查看 641关注 0票数 1

我的问题是如何用我自己的JavaFX8创建一个树型视图(treetableview,Comparator )。

我已经找到了一些像JavaFX TableView排序策略这样的例子,但它们都是TableView,而不是TreeTableView

谢谢。

下面是我的示例代码:

代码语言:javascript
复制
 import java.util.Comparator;
 import javafx.application.Application;
 import javafx.application.Platform;
 import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.beans.property.SimpleStringProperty;
 import javafx.collections.FXCollections;
 import javafx.collections.ObservableList;
 import javafx.geometry.Insets;
 import javafx.scene.Group;
 import javafx.scene.Scene;
 import javafx.scene.control.Label;
 import javafx.scene.control.TreeItem;
 import javafx.scene.control.TreeTableColumn;
 import javafx.scene.control.TreeTableView;
 import javafx.scene.layout.VBox;
 import javafx.scene.text.Font;
 import javafx.stage.Stage;
 import javafx.util.Callback;

public class TableViewSampleWithoutEdit extends Application {
     private TreeTableView<Person> table = new TreeTableView<Person>();
     private ExtraPerson extraPerson = new ExtraPerson("Ninja Village");
     TreeItem root = new TreeItem<>("root");
     private final ObservableList<TreeItem<Person>> data = FXCollections
        .observableArrayList(
                new TreeItem<Person>( new Person("Jacob", "Smith", "jacob.smith@example.com")),
                new TreeItem<Person>( new Person("Isabella", "Johnson","isabella.johnson@example.com")),
                new TreeItem<Person>( new Person("Ethan", "Williams","ethan.williams@example.com")),
                new TreeItem<Person>(  new Person("Emma", "Jones", "emma.jones@example.com")),
                new TreeItem<Person>( new Person("Michael", "Brown", "michael.brown@example.com")),
                new TreeItem<Person>( extraPerson));

public static void main(String[] args) {
    launch(args);
}

@Override
public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Table View Sample");
    stage.setWidth(450);
    stage.setHeight(500);

    final Label label = new Label("Address Book");
    label.setFont(new Font("Arial", 20));

    table.setEditable(true);
    Platform.runLater(() -> {
    TreeTableColumn<Person, String> firstNameCol = new TreeTableColumn("First Name");
    firstNameCol.setMinWidth(100);
    firstNameCol.setCellValueFactory(cellData -> {

        if (cellData.getValue().getValue()instanceof Person) {
            return new ReadOnlyObjectWrapper(cellData.getValue().getValue().getFirstName());

        }
        return new ReadOnlyObjectWrapper(cellData.getValue().getValue());
    });
    TreeTableColumn<Person, String> lastNameCol = new TreeTableColumn("Last Name");
    lastNameCol.setMinWidth(100);
    lastNameCol.setCellValueFactory(cellData -> {
        if (cellData.getValue().getValue()instanceof Person) {

            return new ReadOnlyObjectWrapper(cellData.getValue().getValue().getLastName());

        }
        return new ReadOnlyObjectWrapper(cellData.getValue().getValue());
    });       

    /**
     * Adding comparator to extraPerson
     */

    table.sortPolicyProperty().set(
            new Callback<TreeTableView<Person>, Boolean>() {

                @Override
                public Boolean call(final TreeTableView<Person> param) {
                    Comparator<TreeItem<Person>> comparator = new Comparator<TreeItem<Person>>() {
                        @Override
                        public int compare(TreeItem<Person> r1, TreeItem<Person> r2) {
                            if (r1.getValue() == extraPerson) {
                                return 1;
                            } else if (r2.getValue() == extraPerson) {
                                return -1;
                            } else if (param.getComparator() == null) {
                                return 0;
                            } else {System.out.println("c");
                                return param.getComparator()
                                        .compare(r1, r2);
                            }
                        }
                    };
                    ObservableList<TreeItem<Person>> tables = FXCollections.observableArrayList();
                    for (int i = 0; i < table.getExpandedItemCount(); i++) {
                        tables.add(table.getTreeItem(0));
                    }

                    FXCollections.sort(tables,comparator);
                    if (tables.size()>0) {
                        table.getRoot().getChildren().setAll(tables);
                    }

                    return true;
                }

            });
    root.getChildren().setAll(data);
    table.setRoot(root);
    table.getColumns().addAll(firstNameCol, lastNameCol);
    root.setExpanded(true);
    final VBox vbox = new VBox();
    vbox.setSpacing(5);
    vbox.setPadding(new Insets(10, 0, 0, 10));
    vbox.getChildren().addAll(label, table);

    ((Group) scene.getRoot()).getChildren().addAll(vbox);

    stage.setScene(scene);
    stage.show();
}

public static class Person {

    private final SimpleStringProperty firstName;
    private final SimpleStringProperty lastName;
    private final SimpleStringProperty email;

    private Person(String fName, String lName, String email) {
        this.firstName = new SimpleStringProperty(fName);
        this.lastName = new SimpleStringProperty(lName);
        this.email = new SimpleStringProperty(email);
    }

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String fName) {
        firstName.set(fName);
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String fName) {
        lastName.set(fName);
    }

    public String getEmail() {
        return email.get();
    }

    public void setEmail(String fName) {
        email.set(fName);
    }
}

public static class ExtraPerson extends Person {

    private final SimpleStringProperty address;

    private ExtraPerson(String address) {
        super("Itachi", "Uchiha", "leaf@village.ninja");
        this.address = new SimpleStringProperty(address);
    }

    public String getAddress() {
        return address.get();
    }

    public void setAddress(String address) {
        this.address.set(address);
    }

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-08 17:44:08

换掉这个..。

代码语言:javascript
复制
ObservableList<TreeItem<Person>> tables = FXCollections.observableArrayList();
for (int i = 0; i < table.getExpandedItemCount(); i++) {
    tables.add(table.getTreeItem(0));
}

FXCollections.sort(tables,comparator);
if (tables.size()>0) {
    table.getRoot().getChildren().setAll(tables);
}

..。用这个..。

代码语言:javascript
复制
if(table.getRoot() != null)
    FXCollections.sort(table.getRoot().getChildren(), comparator);

..。而且它会起作用的(至少对你问题中的TreeTableView来说是这样)。

与你在回答中提到的完全相同:

您可以创建一个比较器,用于TreeTableView中的对象集合。在不同的列表中使用这个比较器的唯一不同之处是:在链接的答案中,它用于比较table.getItems(),而我将其修改为用于table.getRoot().getChildren() (根节点的子节点)。

注意,如果您添加了多个level1 TreeItem,或者增加了树级,则必须对其进行调整,但工作流是相同的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37697000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档