如何在JavaFX中重新运行程序

时间:2015-03-21 22:14:24

标签: java javafx filechooser

我有2个控制器用于2个fxml文件。在一个控制器中,我有一个handleOpen函数,它打开一个文件选择器并给出一个我称之为模型的类的路径。然后在另一个控制器上,一个函数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);
}

}

这是主要的,我将两个fxmls组合起来并设置阶段:

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

}

1 个答案:

答案 0 :(得分:0)

问题不在于道路。我不得不重置在前一个程序中初始化的数据。因此,在将路径设置为null之后,我刚刚创建了引用数据的类实例。

...

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

}