在for循环中等待输入的最佳方法是什么?

时间:2012-11-17 17:24:58

标签: java dialog javafx-2

在JavaFX 2中,我通过读取Excel文件填充了TableView。它看起来像这样:

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

当用户按下“导入”按钮时,必须将所有记录添加到数据库中。但是,如果'calved'字段的值为'true',我会显示一个Dialog窗口,用户必须选择一个日期来指定产犊发生的时间。现在最大的问题是,一旦对话窗口打开,我希望我的for循环暂停。使用我当前的代码,所有对话窗口都堆叠在一起。

这是加载FXML的Dialog方法:

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

这是我处理桌子的for循环:

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

在我的setOnCloseRequest()方法中使用showDialog()选项吗?

2 个答案:

答案 0 :(得分:1)

如果将奶牛列表复制到另一个数据结构(例如队列)并在处理过程中删除每头奶牛,则恢复处理它相对容易,因为只有需要处理的奶牛仍然存在。

答案 1 :(得分:0)

似乎答案比我想象的要简单得多,只需使用showAndWait()方法而不是show()。我怎么能错过那个...谢谢你的帮助。

showDialog()方法的最终代码:

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