繁体   English   中英

如何在JavaFX中重新运行程序

[英]How to rerun a program in JavaFX

我有2个fxml文件的2个控制器。 在一个控制器中,我有一个handleOpen函数,该函数可打开文件选择器并提供指向我称为模型的Class的路径。 然后,在另一个控制器上,一个函数treeTableDraw在单击“绘制”按钮并运行程序之后,获取此路径。 我有另一个按钮可以重置程序。 它将结果设置为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和set stage:

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

}

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

...

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

} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM