1

In JavaFX 2 I have a TableView beeing populated by reading an Excel file. It looks like this:

identification    cellcount    calved
o0001             12345        false
o0002             65432        true
o0003             55555        false
...

When users press the 'Import' button, all records have to be added to a database. However, If the 'calved' field has 'true' as value, I show a Dialog window where the users have to select a date to specify when the calving happened. Now the big question is that I want my for loop beeing paused as soon as a Dialog window is open. With my current code, all Dialog windows are stacked on eachother.

This is the Dialog method which loads an FXML:

public void showDialog(String sURL){
    final Stage myDialog = new Stage();
    myDialog.initStyle(StageStyle.UTILITY);
    myDialog.initModality(Modality.APPLICATION_MODAL);

    URL url = getClass().getResource(sURL);

    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
    try {
        Node n = (Node) fxmlloader.load(url.openStream());

        Scene myDialogScene = new Scene(VBoxBuilder.create().children(n).alignment(Pos.CENTER).padding(new Insets(0)).build());

        myDialog.setScene(myDialogScene);
        myDialog.show();
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

And here is the for loop where I handle the tablerows:

@FXML
private void handle_ImportCowDataButton(ActionEvent event) {
    Cows selectedCow;

    for(ImportRow row: tblImport.getItems()){
        selectedCow = null;

        for (Cows cow : olCows) {
            if (cow.getOfficial().equals(row.getCownumber())) {
                selectedCow = cow;
            }
        }

        if (selectedCow != null) {
            if (row.getCalving()) {
                //if cow exists and cow has calved, show dialog window loading addcalving.fxml
                //then the for loop should wait until that dialog window is closed before continuing
                Context.getInstance().setPassthroughObject(selectedCow);
                Context.getInstance().showDialog("/GUI/calving/AddCalving.fxml");
            }
        } else {
            //if cow does not exist, show dialog window loading addcow.fxml
            //then the for loop should wait until that dialog window is closed before continuing
            Context.getInstance().setPassthroughObject(selectedFarmer);
            Context.getInstance().showDialog("/GUI/cow/AddCow.fxml");
        }
    }
}

Is working with setOnCloseRequest() in my showDialog() method an option?

Perneel
  • 3,317
  • 7
  • 45
  • 66
  • `how can I continue the for loop?` -> Don't use `break`?? – Rohit Jain Nov 17 '12 at 17:28
  • I know break is not the sollution, however, even if I don't use it, the Dialogs are just stacked on eachother and the for loop is not waiting for input – Perneel Nov 17 '12 at 17:40
  • what is `Dialog`, is it some library class? – Sergey Grinev Nov 17 '12 at 21:00
  • Hey Sergey, I changed the code for handling the import as it seemed to be a bit messy and unclear. This is how I wrote it at first before trying to pause the for loopt. Dialog was just a class which I was using for troubleshooting :) – Perneel Nov 18 '12 at 08:13
  • http://stackoverflow.com/questions/10347883/creating-a-javafx-dialog-box, may be helpful :) – invariant Nov 21 '12 at 20:27

2 Answers2

1

If you copy the cows list into another data structure such as a queue and remove each cow as it is processed, it is relatively easy to resume processing it since only the cows that need to be processed remain.

Andy Till
  • 3,371
  • 2
  • 18
  • 23
0

It seems the answer was much easier then I thought, simply use the showAndWait() method instead of show(). How on earth could I have missed that... Thanks for the help thou.

Final code of the showDialog() method:

public void showDialog(String sURL){
    final Stage myDialog = new Stage();
    myDialog.initStyle(StageStyle.UTILITY);
    myDialog.initModality(Modality.APPLICATION_MODAL);

    URL url = getClass().getResource(sURL);

    FXMLLoader fxmlloader = new FXMLLoader();
    fxmlloader.setLocation(url);
    fxmlloader.setBuilderFactory(new JavaFXBuilderFactory());
    try {
        Node n = (Node) fxmlloader.load(url.openStream());

        Scene myDialogScene = new Scene(VBoxBuilder.create().children(n).alignment(Pos.CENTER).padding(new Insets(0)).build());

        myDialog.setScene(myDialogScene);
        myDialog.showAndWait();
    } catch (Exception ex) {
        System.out.println(ex);
    }
}
Perneel
  • 3,317
  • 7
  • 45
  • 66