【问题标题】:How to rerun a program in JavaFX如何在 JavaFX 中重新运行程序
【发布时间】:2015-05-25 04:04:21
【问题描述】:

我有 2 个控制器用于 2 个 fxml 文件。在一个控制器中,我有一个handleOpen 函数,它打开一个文件选择器并给出一个我称之为模型的类的路径。然后在另一个控制器上,treeTableDraw 获得这个路径,点击 Draw Button 并运行程序。我有另一个按钮来重置程序。它将结果设置回 null,但是当打开另一个文件运行时,程序崩溃,因为路径为 null。如何重置程序并使其使用从打开文件选择器中选择的新路径?

//Gets the path from model and runs the program
public void treeTableDraw(ActionEvent event) {

    new Controller(model.getText());
    drawTable();
    numberOfFunctions = dc.getFuncAll().size();
    numberOfOrganizations = dc.getSortedAssignedOrg().size();
    funcLabel.setText(numberOfFunctions + "");
    orgLabel.setText(numberOfOrganizations + "");
    btnDraw.setDisable(true);

}

/**
 * Clrears TreeTableView and sets back labels
 * 
 * @param event
 */
public void treeTableReset(ActionEvent event) {
    btnDraw.setDisable(false);
    model.setText(null);
    funcLabel.setText("0");
    orgLabel.setText("0");
    treeTable.getColumns().clear();

}

这是具有打开文件功能的 RootLayout 类:

@FXML
private void handleOpen() {

    FileChooser fileChooser = new FileChooser();

    // Set extension filter
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter(
            "3lgm2 files (*.z3lgm)", "*z3lgm");
    fileChooser.getExtensionFilters().add(extFilter);

    // Show open file dialog
    File file = fileChooser.showOpenDialog(main.getPrimaryStage());

    if (file != null) {
        path = file.toString();

        model.setText(path);

    }

}

这里是模型类

public class Model {
private final StringProperty text = new SimpleStringProperty();

public StringProperty textProperty() {
    return text;
}

public final String getText() {
    return textProperty().get();
}

public final void setText(String text) {
    textProperty().set(text);
}

}

这是主要的,我将两个 fxml 组合起来并设置舞台:

public class Main extends Application {
private Stage primaryStage;
private BorderPane rootLayout;
private Model model = new Model();


@Override
public void start(Stage primaryStage) {

    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("IT-Saturation");
    initRootLayout();
    showOverView();

}

private void showOverView() {
    try {
        FXMLLoader loader = new FXMLLoader();

        loader.setLocation(Main.class.getResource("/view/OverView.fxml"));
        loader.setController(new OverViewController(model));

        AnchorPane overView = (AnchorPane) loader.load();
        rootLayout.setCenter(overView);

    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void initRootLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("/view/RootLayout.fxml"));
        loader.setController(new RootLayoutController(model));

        rootLayout = (BorderPane) loader.load();
        // show scene containing the root layout
        Scene scene = new Scene(rootLayout);
        scene.getStylesheets().add(
                getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        // gives controller access to main
        RootLayoutController controller = loader.getController();
        controller.setMainApp(this);
        primaryStage.show();

    } catch (IOException e) {

        e.printStackTrace();
    }

}


/**
 * Returns the main stage.
 * 
 * @return primaryStage
 */
public Stage getPrimaryStage() {

    return primaryStage;
}

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

public void showMostComputerizedStatistics() {
    try {
        // Load the fxml file and create a new stage for the popup.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class
                .getResource("view/BirthdayStatistics.fxml"));
        AnchorPane page = (AnchorPane) loader.load();
        Stage dialogStage = new Stage();
        dialogStage.setTitle("Birthday Statistics");
        dialogStage.initModality(Modality.WINDOW_MODAL);
        dialogStage.initOwner(primaryStage);
        Scene scene = new Scene(page);
        dialogStage.setScene(scene);

        // Set the persons into the controller.
        MostComputerizedController controller = loader.getController();

        dialogStage.show();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

【问题讨论】:

  • 如果你使用:File file = fileChooser.showOpenDialog(new Stage()); 会有什么不同吗?
  • 也许path = file.getAbsolutePath(); 可以解决问题,但很难说,因为我看不出哪里出了问题。
  • 我只能再次运行同一个文件。当我选择一个新文件时,程序不会被初始化。
  • 我以为当我执行 setText(null) 时路径也会被重置,但显然不是这样。
  • 您是否尝试过不将 setText 设置为 null 并保留它,当您打开一个新的文件选择器对话框时,它会在选择新文件时将文本设置为其他内容。也许添加chooser.setInitialDirectory("\\users\\yourname\\); 或类似的东西。

标签: java javafx filechooser


【解决方案1】:

问题不在于路径。我不得不重置在前一次运行程序时初始化的数据。因此,在将路径设置为 null 之后,我刚刚更新了引用数据的类的实例。

...

public void treeTableReset(ActionEvent event) {
    btnDraw.setDisable(false);
    //model.setText(null);
    funcLabel.setText("0");
    orgLabel.setText("0");
    treeTable.getColumns().clear();
    dc = new DataConstructor();

} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 2019-03-24
    相关资源
    最近更新 更多