2016-08-03 20 views
0

これはJFXテーブルに関するものです。 私は上記のリンクを読んで、それは私の仕事には良いことでした。 私の要件は - テーブルの列見出しをクリックし、データが昇順または降順でソートされているとします。アプリケーションを再起動してもソートされたデータをテーブルに保持したい。誰かがこの問題について私を助けてくれますか?列ヘッダー名と昇順/降順を覚えておき、初期化時に並べ替えるにはどうすればよいですか? Sorting order of jfx table clumnjfxテーブルの列の並べ替え順序を維持する方法

答えて

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

} 
関連する問題