16

我正在玩FilteredList他们添加回 JDK8 的类,感觉快了很多 - 似乎让性能更接近 GlazedLists。FilteredList但是,当我使用 a而不是 a时,表列排序似乎根本不起作用ObservableList

控制台中没有异常/堆栈跟踪。

这是我的控制器中的实例成员:

    private ObservableList<Film> masterData = FXCollections.observableArrayList();
    private FilteredList<Film> filteredData;

我的控制器初始化:

@FXML
void initialize() {
   ...   
   filmTable.setItems(filteredData);
   ...
}

构造函数:

public FilmBrowserController() {

   ...
   masterData.addAll(filmRepository.findAllSortedByTitle());
   filteredData = new FilteredList<>(masterData);
   filteredData.setPredicate(film -> true);
}

当过滤器 TextField 更改时,我一直在通过设置一个新谓词来过滤列表:

filteredData.setPredicate(film ->
     film.getTitle().toLowerCase().contains(filterField.getText().toLowerCase()));

我对 JavaFX 仍然很陌生——我是否缺少一些非常基本的东西,或者这可能是一个错误?我正在使用这里下载的 JDK 8:https : //jdk8.java.net/download.html Build b100。

是否不可能有一个可以过滤但同时按列排序的表?

编辑:

修改了我在 stackoverflow 上找到的另一个 javaFX 表示例,我添加了这一行(74):

table.setItems(table.getItems().filtered(p -> p.getFirstName().startsWith("a")));

完整来源:

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.Random;

public class TableSortPerformanceTest extends Application {

    public static final int INIT_LIST_SIZE = 100_000;

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

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

        final TableView<Person> table = new TableView<Person>();
        table.setEditable(true);

        TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));
        firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<Person, String> cdf) {
                return cdf.getValue().firstNameProperty();
            }
        });

        TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));



        TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));



        final Random random = new Random();
        for (int i = 0; i < INIT_LIST_SIZE; i++) {
            table.getItems().add(new Person(randomString(random), randomString(random), randomString(random)));
        }
        table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));

        table.setItems(table.getItems().filtered(p -> p.getFirstName().startsWith("a")));

        long start = new Date().getTime();
//        Collections.sort(table.getItems());
        long end   = new Date().getTime();
        System.out.println("Took: " + (end - start));


        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("First Name");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("Last Name");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("Email");

        final Button addButton = new Button("Add");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                String firstName = isEmpty(addFirstName.getText()) ? randomString(random) : addFirstName.getText();
                String lastName  = isEmpty(addLastName.getText())  ? randomString(random) : addLastName.getText();
                String email     = isEmpty(addEmail.getText())     ? randomString(random) : addEmail.getText();
                Person person = new Person(firstName, lastName, email);
                int idx = Collections.binarySearch(table.getItems(), person);
                if (idx < 0) {
                    idx = -idx - 1;
                }
                table.getItems().add(idx, person);
                table.getSelectionModel().select(idx);
                table.scrollTo(idx);

                addFirstName.clear();
                addLastName.clear();
                addEmail.clear();
            }
        });

        final HBox hb = new HBox(3);
        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10));
        vbox.getChildren().addAll(label, table, hb);

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

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

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

    private boolean isEmpty(String string) {
        return (string == null || string.isEmpty());
    }

    private String randomString(Random random) {
        char[] chars = new char[20];
        for (int i = 0; i < 20; i++) {
            int nextInt = random.nextInt(26);
            nextInt += random.nextBoolean() ? 65 : 97;
            chars[i] = (char) nextInt;
        }
        return new String(chars);
    }

    public static class Person implements Comparable<Person> {

        private final StringProperty firstName;
        private final StringProperty lastName;
        private final StringProperty 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 StringProperty firstNameProperty() {
            return firstName ;
        }

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

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

        public StringProperty lastNameProperty() {
            return lastName ;
        }

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

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

        public StringProperty emailProperty() {
            return email ;
        }

        @Override
        public int compareTo(Person o) {
            return firstName.get().compareToIgnoreCase(o.getFirstName());
        }
    }
}

我现在为此https://javafx-jira.kenai.com/browse/RT-32091创建了一张票

4

2 回答 2

15

要通过列标题对TableView进行排序,请执行以下操作:tableView

ObservableList<Film> masterData = // ... Some observable list
FilteredList<Film> filteredData = new FilteredList(masterData);
SortedList<Film>   sortableData = new SortedList<>(filteredData);
tableView.setItems(sortableData);
sortableData.comparatorProperty().bind(tableView.comparatorProperty());

过滤TableView 的内容,只需:

filteredData.setPredicate(x -> x.getTitle().contains("The Walki"));

要根据 TextField实时过滤textField内容:

textField.textProperty().addListener((observa,old,neo)->
     filteredData.setPredicate(x -> x.getTitle().contains(neo))
);
于 2016-11-23T19:54:55.380 回答
12

事实证明这还不支持(https://javafx-jira.kenai.com/browse/RT-32091) - 但应该在未来的版本中:

马丁·斯拉德切克

FilteredList 是不可修改的,因此无法排序。为此,您还需要将其包装在 SortedList 中。此处的简短教程:http: //fxexperience.com/2013/08/returning-a-tableview-back-to-an-unsorted-state-in-javafx-8-0/

我正在将其更改为进行调整,因为我正在考虑使 FXCollections.sort() 方法能够识别 TransformationList,因此可以对 FilteredList 进行排序。由于 TableView 正在使用这种方法,它应该在实现后立即工作。

添加代码示例:

package com.test;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.collections.transformation.SortedList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Callback;

import java.util.*;

public class TableFilterTest extends Application {

    public static final int INIT_LIST_SIZE = 100_000;

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

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

        final TableView<Person> table = new TableView<Person>();
        table.setEditable(true);

        TableColumn<Person, String> firstNameCol = new TableColumn<Person, String>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("firstName"));

        firstNameCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,String>, ObservableValue<String>>() {
            @Override
            public ObservableValue<String> call(CellDataFeatures<Person, String> cdf) {
                return cdf.getValue().firstNameProperty();
            }
        });

        TableColumn<Person, String> lastNameCol = new TableColumn<Person, String>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("lastName"));

        TableColumn<Person, String> emailCol = new TableColumn<Person, String>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(
                new PropertyValueFactory<Person, String>("email"));


        final Random random = new Random();

        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < INIT_LIST_SIZE; i++) {
            personList.add(new Person(randomString(random), randomString(random), randomString(random)));
        }

        ObservableList<Person> personLists = FXCollections.observableList(personList);
        table.getColumns().addAll(Arrays.asList(firstNameCol, lastNameCol, emailCol));


        final FilteredList<Person> filteredList  = personLists.filtered(p -> p.getFirstName().startsWith("a"));


        SortedList<Person> personSortedList = new SortedList<>(filteredList);

        table.setItems(personSortedList);
        personSortedList.comparatorProperty().bind(table.comparatorProperty());

        long start = new Date().getTime();
        long end   = new Date().getTime();
        System.out.println("Took: " + (end - start));

        final TextField addFirstName = new TextField();
        addFirstName.setPromptText("First Name");
        addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
        final TextField addLastName = new TextField();
        addLastName.setMaxWidth(lastNameCol.getPrefWidth());
        addLastName.setPromptText("Last Name");
        final TextField addEmail = new TextField();
        addEmail.setMaxWidth(emailCol.getPrefWidth());
        addEmail.setPromptText("Email");

        final TextField filterText = new TextField();
        filterText.setMaxWidth(emailCol.getPrefWidth());
        filterText.setPromptText("Filter");
        filterText.textProperty().addListener((observable, oldValue, newValue) ->
                filteredList.setPredicate(p -> p.getFirstName().startsWith(filterText.getText()))
        );

        final Button addButton = new Button("Add");
        addButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                String firstName = isEmpty(addFirstName.getText()) ? randomString(random) : addFirstName.getText();
                String lastName  = isEmpty(addLastName.getText())  ? randomString(random) : addLastName.getText();
                String email     = isEmpty(addEmail.getText())     ? randomString(random) : addEmail.getText();
                Person person = new Person(firstName, lastName, email);
                int idx = Collections.binarySearch(table.getItems(), person);
                if (idx < 0) {
                    idx = -idx - 1;
                }
                table.getItems().add(idx, person);
                table.getSelectionModel().select(idx);
                table.scrollTo(idx);

                addFirstName.clear();
                addLastName.clear();
                addEmail.clear();
            }
        });

        final HBox hb = new HBox(3);



        hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton, filterText);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10));
        vbox.getChildren().addAll(label, table, hb);

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

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

    }

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

    private boolean isEmpty(String string) {
        return (string == null || string.isEmpty());
    }

    private String randomString(Random random) {
        char[] chars = new char[20];
        for (int i = 0; i < 20; i++) {
            int nextInt = random.nextInt(26);
            nextInt += random.nextBoolean() ? 65 : 97;
            chars[i] = (char) nextInt;
        }
        return new String(chars);
    }

    public static class Person implements Comparable<Person> {

        private final StringProperty firstName;
        private final StringProperty lastName;
        private final StringProperty 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 StringProperty firstNameProperty() {
            return firstName ;
        }

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

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

        public StringProperty lastNameProperty() {
            return lastName ;
        }

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

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

        public StringProperty emailProperty() {
            return email ;
        }

        @Override
        public int compareTo(Person o) {
            return firstName.get().compareToIgnoreCase(o.getFirstName());
        }
    }
}
于 2013-08-14T09:21:49.303 回答