更新时无法刷新javafx中的场景/节点

62lalag4  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(366)

我是javafx新手。我试图编写一个简单的gui,但我面临着那些可能与之相关的问题。我使用文件选择器设置文件,并希望执行非常基本的操作:
保存上次使用的文件夹
在vbox中写入所选文件的名称
下面是我的代码(编译):

import java.io.File;
import java.io.IOException;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Main extends Application {
    public static Stage primaryStageS;
    public static Scene mainScene;

    public void start(Stage primaryStage) throws Exception {
        primaryStage.setScene((new Test(primaryStage).getScene()));
        primaryStageS = primaryStage;
        primaryStage.setTitle("Parcel Manager Main Page");
        primaryStage.initStyle(StageStyle.DECORATED);
        VBox main = new VBox(new Label("Test program"));
        mainScene = new Scene(main, 800, 600);
        primaryStage.setScene((new Test(primaryStage)).getScene());
        primaryStage.show();
    }

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

    public class Object1 {
        String name;

        public Object1(File f) throws IOException {
            name = f.getName();
        }

        public String getName() {
            return name;
        }
    }

    public class Test {
        Object1 collec;
        String collecName;
        File lastFolder;
        Pane rootGroup;

        public Test(Stage stage) {
            setButtons(stage);
        }

        public void setButtons(Stage stageGoal) {
            VBox vbox = new VBox();
            Button b = getButton(stageGoal);
            vbox.getChildren().addAll(b, new Label(getCollecName() == null ? "no name" : collecName));
            final GridPane inputGridPane = new GridPane();
            GridPane.setConstraints(vbox, 0, 0);
            inputGridPane.getChildren().addAll(vbox);
            rootGroup = new VBox(12);
            rootGroup.getChildren().addAll(inputGridPane);
            rootGroup.setPadding(new Insets(12, 12, 12, 12));
        }

        public Button getButton(Stage stage) {
            FileChooser fileChooserParcel = new FileChooser();
            fileChooserParcel.setInitialDirectory(getLastFolder());
            Button button = new Button("Select a File");
            button.setOnAction(e -> {
                File f = fileChooserParcel.showOpenDialog(stage);
                if (f != null) {
                    try {
                        collec = new Object1(f);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    setLastFolder(f.getParentFile());
                    setCollecName(collec);
                    setButtons(stage); // tried to reload every buttons - doesn't work
                    stage.setWidth(stage.getWidth() + 0.0001); // found this dirty hack but doesn't work
                }
            });
            return button;
        }

        public void setCollecName(Object1 o1) {
            collecName = o1.getName();
        }

        public String getCollecName() {
            return collecName;
        }

        public File getLastFolder() {
            return lastFolder;
        }

        public void setLastFolder(File folder) {
            System.out.println("set last folder: " + folder);
            lastFolder = folder;
        }

        private Scene getScene() {
            return new Scene(rootGroup, 800, 600);
        }
    }
}

我无法刷新节点以设置当前初始目录或在vbox上显示collecname。我试图通过重新加载对象或调整窗口大小来重新生成它们,但没有任何效果。当我在控制台上打印变量时,我看到它们发生了变化。但是没有为我的任何对象找到任何刷新方法。
我敢打赌这是一个设计程序的问题,但我已经移动了过去一周的东西,不知道如何解决这个问题。
谢谢!

utugiqy6

utugiqy61#

您只需设置一次初始目录。我猜你想每次点击 button . 因此,将这行代码移到处理程序内部。
比较下面的 getButton() 你的方法。

public Button getButton(Stage stage) {
    FileChooser fileChooserParcel = new FileChooser();
    Button button = new Button("Select a File");
    button.setOnAction(e -> {
        fileChooserParcel.setInitialDirectory(getLastFolder()); // CHANGE HERE.
        File f = fileChooserParcel.showOpenDialog(stage);
        if (f != null) {
            try {
                collec = new Object1(f);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            setLastFolder(f.getParentFile());
            setCollecName(collec);
            setButtons(stage); // tried to reload every buttons - doesn't work
            stage.setWidth(stage.getWidth() + 0.0001); // found this dirty hack but doesn't work
        }
    });
    return button;
}

相关问题