1

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

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

当用户按下“导入”按钮时,所有记录都必须添加到数据库中。但是,如果“产犊”字段的值为“真”,我会显示一个对话框窗口,用户必须在其中选择一个日期来指定产犊发生的时间。现在最大的问题是我希望我的 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);
    }
}

这是我处理 tablerows 的 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()方法中使用是一种选择吗?

4

2 回答 2

1

如果您将奶牛列表复制到另一个数据结构(例如队列)中,并在处理每头奶牛时删除它,那么恢复处理它相对容易,因为只剩下需要处理的奶牛。

于 2012-11-17T22:50:24.597 回答
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);
    }
}
于 2012-11-21T20:20:39.927 回答