2016-08-03 49 views
0

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

回答

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

}