0

I have a reset and Draw button on a slide panel. I choose a desired file from file chooser, which is in RootLayout class, and pass the file path to a controller class. Then it does some processes and initializes field in DataCunstructor class. By clicking on Draw a TreeTableView will be shown on slide pane, which is in MainController class. When I click my reset button the table will be cleared but I do not know how to reset the chosen path. After reseting if I click Draw again the same treetable comes up. and If I choose another file and hit Draw, the program breaks. How can I reset all fields including the path to null, and be able to choose another file and process that one?

Here is my Draw and Reset in MainController class:

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

}

My File Chooser in RootLayout class:

@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;
}

I add OverView at the center of rootlayout here at main class:

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);
    }
}
Iman
  • 769
  • 1
  • 13
  • 51

1 Answers1

1

Just declare a reset() in your RootLayout class:

public class RootLayout {

    private Path path;

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

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

Never construct the constructor using the keyword new, always get it from the FXMLLoader.

public class MainController {

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

}
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • thanks for the advise. But I still have the problem. Now I get Nullpointer error for rlc.reset();. Do you know why is that? – Iman Mar 15 '15 at 12:35
  • How and where are you loading the FXML? Can you add it in your question? – ItachiUchiha Mar 15 '15 at 13:38
  • Are you trying `controller` instead of `rlc`, because I can see that you have `RootLayoutController controller = loader.getController();` in your example. Moreover, did you add the reset method in the controller? – ItachiUchiha Mar 15 '15 at 14:54
  • I have added the reset in RootLayOutController, where the file chooser exist. The Controller class is just a class, which interfers between my model and view. It has a constructor Controller(String path) which gets called from file chooser, after choosing a file. – Iman Mar 15 '15 at 15:12
  • when i try RootLayoutController rlc = new RootLayoutController(); rlc.reset(); in treeTableReset(ActionEvent event) instead of calling loader, rlc throws no nullexception but when I try to click Draw on another file, except of the one which has been already processed i get error. – Iman Mar 15 '15 at 15:15