如何坚持jfx表列的排序顺序

时间:2016-08-03 08:25:03

标签: javafx

这是关于JFX表。 我看了上面的链接,这对我的工作很有帮助。 我的要求是 - 假设我点击表列标题,数据按升序或降序排序。即使重新启动我的应用程序,我仍希望在我的表上保留已排序的数据。有人可以帮我解决这个问题吗?如何记住列标题名称和升序/降序并在初始化时对其进行排序? Sorting order of jfx table clumn

1 个答案:

答案 0 :(得分:2)

您只需存储必要的内容即可在用户目录中的文件中恢复排序。以下代码通过为要排序的列数存储int,然后为每列存储intTableColumn.SortType来执行此操作。 int表示初始列索引,SortType表示排序是升序还是降序。

实施例

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class SaveAndRestore extends Application {

    private static final Path CONFIG_FILE;

    static {
        CONFIG_FILE = Paths.get(System.getProperty("user.home"), "myapp", "config.ser");
        Path dir = CONFIG_FILE.getParent();
        if (!Files.exists(dir)) {
            try {
                Files.createDirectory(dir);
            } catch (IOException ex) {
                throw new IllegalStateException("Could not create settings directory.", ex);
            }
        }
    }

    private static final InputStream getConfigReadStream() throws IOException {
        return Files.exists(CONFIG_FILE) ? Files.newInputStream(CONFIG_FILE) : null;
    }

    private static final OutputStream getConfigWriteStream() throws IOException {
        return Files.newOutputStream(CONFIG_FILE);
    }

    @Override
    public void stop() throws Exception {
        try (ObjectOutputStream oos = new ObjectOutputStream(getConfigWriteStream())) {
            oos.writeInt(tableView.getSortOrder().size());
            for (TableColumn tc : tableView.getSortOrder()) {
                oos.writeInt((Integer) tc.getUserData());
                oos.writeObject(tc.getSortType());
            }
        }
    }

    public static class Item {

        private final IntegerProperty number = new SimpleIntegerProperty();
        private final StringProperty string = new SimpleStringProperty();

        public Item(int number, String string) {
            this.number.set(number);
            this.string.set(string);
        }

        public final int getNumber() {
            return this.number.get();
        }

        public final void setNumber(int value) {
            this.number.set(value);
        }

        public final IntegerProperty numberProperty() {
            return this.number;
        }

        public final String getString() {
            return this.string.get();
        }

        public final void setString(String value) {
            this.string.set(value);
        }

        public final StringProperty stringProperty() {
            return this.string;
        }
    }

    private static <T> TableColumn<Item, T> createColumn(String property) {
        TableColumn<Item, T> column = new TableColumn<>(property);
        column.setCellValueFactory(new PropertyValueFactory(property));
        return column;
    }

    private TableView<Item> tableView;

    @Override
    public void start(Stage primaryStage) throws IOException, ClassNotFoundException {
        tableView = new TableView<>(FXCollections.observableArrayList(
                new Item(10, "Hello World"),
                new Item(5, "Zyzz"),
                new Item(20, "Aaron")
        ));

        tableView.getColumns().addAll(createColumn("number"));
        tableView.getColumns().addAll(createColumn("string"));
        for (int i = 0, size = tableView.getColumns().size(); i < size; i++) {
            tableView.getColumns().get(i).setUserData(i);
        }

        // restore state from config
        InputStream is = getConfigReadStream();
        if (is != null) {
            try (ObjectInputStream ois = new ObjectInputStream(is)) {
                for (int num = ois.readInt(); num > 0; num--) {
                    TableColumn<Item, ?> column = tableView.getColumns().get(ois.readInt());
                    column.setSortType((TableColumn.SortType) ois.readObject());
                    tableView.getSortOrder().add(column);
                }
            }
        }

        Scene scene = new Scene(tableView);

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

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

}