2017-05-31 59 views
1

我嘗試了幾種方法,包括jaxb和序列化。兩者都沒有真正的工作。舞臺/場景中的node包括來自richTextFX的Label,BarChartCodeArea。對於序列化方法,我得到了java.io.NotSerializableException: javafx.scene.chart.BarChart。有沒有辦法將場景/舞臺保存到像xml這樣的文件(而不是FXML)?那麼即使我關閉了應用程序,我也可以恢復場景/舞臺。非常感謝。Javafx將舞臺/場景保存到文件中

+2

您應該考慮保存* model *(即應用程序顯示的基礎數據),而不是保存實際的UI。 –

回答

0

James_D評論,你應該從你的UI元素保存數據不元素themselves'cos你有他們已編碼,右)

的我做它的方式一小片段:

public class SaveRestoreDemo extends Application { 

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

    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("Save/restore demo"); 
     Group root = new Group(); 
     Scene scene = new Scene(root, 600, 250, Color.WHITE); 

     BorderPane borderPane = new BorderPane(); 
     borderPane.prefHeightProperty().bind(scene.heightProperty()); 
     borderPane.prefWidthProperty().bind(scene.widthProperty()); 

     VBox box = new VBox(10); 
     TextArea area = new TextArea(); 
     area.setId("textArea"); 

     TextField field = new TextField(); 
     field.setId("field"); 

     box.getChildren().addAll(area, field); 

     borderPane.setTop(getMainMenuContainer(box.getChildren())); 
     borderPane.setCenter(box); 
     root.getChildren().add(borderPane); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    private VBox getMainMenuContainer(ObservableList<Node> children) { 
     VBox topContainer = new VBox(); 
     MenuBar mainMenu = new MenuBar(); 
     topContainer.getChildren().add(mainMenu); 
     Menu menu = new Menu("Menu"); 

     MenuItem load = new MenuItem("Load"); 
     load.setOnAction(e -> load(children)); 

     MenuItem save = new MenuItem("Save"); 
     save.setOnAction(e -> save(children)); 

     menu.getItems().addAll(save, load); 

     mainMenu.getMenus().add(menu); 
     return topContainer; 
    } 

    private void load(ObservableList<Node> children) { 
     FileChooser fileChooser = new FileChooser(); 
     fileChooser.setTitle("Load file"); 
     File file = fileChooser.showOpenDialog(new Stage()); 
     if (file != null) { 
      try { 
       Values values = new Gson().fromJson(org.apache.commons.io.FileUtils.readFileToString(file, StandardCharsets.UTF_8), Values.class); 
       children.stream().filter(child -> child.getId() != null) 
         .forEach(child -> { 
          if (child instanceof TextField) { 
           TextField field = (TextField) child; 
           field.setText(values.getFieldText()); 
          } else if (child instanceof TextArea) { 
           TextArea area = (TextArea) child; 
           area.setText(values.getAreaText()); 
          } 
         }); 
      } catch (IOException e) { 
       // handle properly 
      } 
     } 
    } 

    private void save(ObservableList<Node> children) { 
     FileChooser fileChooser = new FileChooser(); 
     fileChooser.setTitle("Save file"); 
     File file = fileChooser.showSaveDialog(new Stage()); 
     if (file != null) { 
      Values values = new Values(); 
      children.stream().filter(child -> child.getId() != null) 
        .forEach(child -> { 
         if (child instanceof TextField) { 
          TextField field = (TextField) child; 
          values.setFieldText(field.getText()); 
         } else if (child instanceof TextArea) { 
          TextArea area = (TextArea) child; 
          values.setAreaText(area.getText()); 
         } 
        }); 

      try { 
       org.apache.commons.io.FileUtils.write(file, new Gson().toJson(values), StandardCharsets.UTF_8); 
      } catch (IOException e) { 
       // handle properly 
      } 
     } 
    } 

    private static class Values { 
     private String areaText; 
     private String fieldText; 

     public String getAreaText() { 
      return areaText; 
     } 

     public void setAreaText(String areaText) { 
      this.areaText = areaText; 
     } 

     public void setFieldText(String fieldText) { 
      this.fieldText = fieldText; 
     } 

     public String getFieldText() { 

      return fieldText; 
     } 
    } 
} 

基本上,我只是循環通過主容器的孩子,從他們那裏收集值,然後使用Gson和Apache Commons保存到Json。 這個例子很簡單,對於更復雜的保存\加載,你可以使用節點的id。例如,您可以保存ID列表或地圖,然後在保存和加載時將它們用作標識符。