如何在JavaFx中重置文件选择器中的选定路径

时间:2015-03-15 11:41:18

标签: java javafx filechooser

我在幻灯片面板上有一个重置和绘图按钮。我从文件选择器中选择了一个所需的文件,该文件选择器位于RootLayout类中,并将文件路径传递给控制器​​类。然后它执行一些过程并初始化DataCunstructor类中的字段。通过单击Draw,TreeTableView将显示在幻灯片窗格中,该窗格位于MainController类中。当我单击我的重置按钮时,表格将被清除,但我不知道如何重置所选路径。重新设置后,如果我再次单击Draw,则会出现相同的treetable。如果我选择另一个文件并点击Draw,程序会中断。 如何重置包括路径为null的所有字段,并能够选择另一个文件并处理该文件?

这是我在MainController类中的Draw和Reset:

public void treeTableDraw(ActionEvent event) {

    drawTable();//creates the TreeTableView
    numberOfFunctions= dc.getFuncAll().size(); 
    numberOfOrganizations = dc.getSortedAssignedOrg().size();
    funcLabel.setText(numberOfFunctions+"");//set Lable value
    orgLabel.setText(numberOfOrganizations + "");//set Lable value

}
public void treeTableReset(ActionEvent event){

    funcLabel.setText("0");//reset Label
    orgLabel.setText("0");
    treeTable.getColumns().clear(); //clears columns (TreeTable)
    ///////////////////////////////////////
    //non of the following did the path reset//
    ///////////////////////////////////////
    //dc = new DataConstructor();
    //Controller controller = new Controller();
    //controller.setPath(null);
    RootLayoutController rlc = loader.getController();
    rlc.reset();

}

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 save file dialog
    File file = fileChooser.showOpenDialog(main.getPrimaryStage());
    path = file.toString();
    if (path != null) {

        new Controller(path);

    }
}
public void reset(){
path = null;
}

我在主要类的rootlayout中心添加了OverView:

public class Main extends Application {
    private Stage primaryStage;
    private BorderPane rootLayout;

    //private ObservableList<DataConstructor> treeTableData = FXCollections.observableArrayList();

    @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"));
        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"));
            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 app
            RootLayoutController controller = loader.getController();
            controller.setMainApp(this);
            primaryStage.show();
        } catch (IOException e) {

            e.printStackTrace();
        }
        File file = getFilePath();
        if (file != null) {
            loadDataFromFile(file);
        }


    }

    /**
     * Returns the main stage.
     * @return primaryStage
     */
    public Stage getPrimaryStage() {
        return primaryStage;
    }
    public static void main(String[] args) {
        launch(args);
    }
}

1 个答案:

答案 0 :(得分:1)

只需在reset()课程中声明RootLayout

public class RootLayout {

    private Path path;

    @FXML
    private void handleOpen() {
        ...
        path = file.toString();
    }

    public void reset() {
        path= null;
    }
}

永远不要使用关键字new构造构造函数,始终从FXMLLoader获取它。

public class MainController {

    ...
    RootLayoutController controller = loader.getController();
    controller.reset();
    ...

}