首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javafx将舞台/场景保存到文件中

Javafx将舞台/场景保存到文件中
EN

Stack Overflow用户
提问于 2017-05-31 14:56:26
回答 1查看 2.9K关注 0票数 1

我尝试过几种方法,包括jaxb和序列化。两者都不起作用。舞台/场景中的node包括来自richTextFX的LabelBarChartCodeArea。对于序列化方法,我得到了java.io.NotSerializableException: javafx.scene.chart.BarChart。是否有一种方法可以将场景/阶段保存到xml之类的文件中(而不是只说FXML )?然后,即使我关闭了应用程序,也可以恢复场景/阶段。非常感谢。

EN

回答 1

Stack Overflow用户

发布于 2017-05-31 19:15:42

根据James_D注释,您应该保存UI元素中的数据,而不是元素本身,因为已经对它们进行了编码,对吗?)

这是我做这件事的一小部分:

代码语言:javascript
复制
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;
        }
    }
}

基本上,我只是循环遍历主容器的子容器,从它们收集值,然后使用葛森和Apache保存。这个例子非常简单,对于更复杂的保存\加载,您可以使用节点的id。例如,您可以保存ids的列表或映射,然后在保存和加载时使用它们作为标识符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44288099

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档