无法关闭 Java 确认子窗口 (Vaadin 8)

Cannot close Java confirmation subwindow (Vaadin 8)

提问人:Cristian Sfetcu 提问时间:8/16/2021 最后编辑:Cristian Sfetcu 更新时间:8/16/2021 访问量:61

问:

我有一个按钮,应该生成一个包含一些数据的 Excel 文件。 我需要添加一个弹出窗口,询问“你确定吗?”,带有 2 个按钮:“是”或“否”。

我有一个按钮,我添加了一个clicklistener,在clicklistener中,我从我创建的类(YesNoConfirmation)初始化了一个对象。
YesNoConfirmation 创建一个包含字符串和 2 个按钮的 Window,并将其添加到主窗口。我已将 clicklisteners 添加到确认窗口的 2 个按钮中。我的 YesNoConfirmation 类有一个名为“isConfirmed”的成员,它应该保持状态 - 如果已确认或否。YES/NO 按钮中的 2 个 clicklisteners 更新我的类的 isConfirmed 成员。然后,根据“isConfirmed”的值,它应该决定是否执行生成 Excel 文件的代码。我已经做了一些调试,似乎“isConfirmed”成员得到了相应的更新,但是当我检查用户是否单击了“是”或“否”时,什么也没发生。
if(panel1.getConfirmationStatus() == true)

你能帮帮我吗?

来自主应用程序的代码是:

        getBtnGenereazaNOTAAUTORIZARE().addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ");
                //ListDataProvider<LinkedHashMap<String, String>> rapoarte=(ListDataProvider<LinkedHashMap<String, String>>) getGrdRapoarteDetalii().getDataProvider();
                //ArrayList<LinkedHashMap<String, String>> rapoarteList=(ArrayList<LinkedHashMap<String, String>>) rapoarte.getItems();
                File xlsFile = new File("/home/misadmin/rapoarte/Nota_Autoriz_Ch_DL_Anexa9a.xlsx");
                SelectBeanStream result = new SelectBeanStream();//"ok";
                InputStream is;
                YesNoConfirm panel1 = new YesNoConfirm();
                panel1.openConfirmationPanel("Confirmare");
                if(panel1.getConfirmationStatus() == true) {
                    try {
                        //String fisierNume=prj_cod+"_"+TipRap+"_"+NR_Rap+"_"+(new Date());
                        String pattern = "yyyy-MM-dd HH:mm:ss";
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                        String fisierNume=prj_cod+"_RF_"+NR_Rap+"-NotaAutorizare_"+(simpleDateFormat.format(new Date())).replace(" ", "_").replaceAll(":", "_");
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls fisierNume "+fisierNume);
                        //fisierNume=fisierNume.replaceAll(" ", "_").replaceAll(":", "_");
                        result = GrantulLocalServiceUtil.dl_Cheltuieli_NA(xlsFile, fisierNume, prj_cod, String.valueOf(NR_Rap));
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls result "+result);
                        is=result.getStream();
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls is "+is);
                        if(log.isDebugEnabled())log.debug("getBtnGenereazaNOTAAUTORIZARE ctrlExportXls is.available() "+is.available());
                        byte[] targetByte = new byte[is.available()];
                        is.read(targetByte);
                        
                        showFile(getBtnGenereazaNOTAAUTORIZARE().getUI(), fisierNume, targetByte, "XLSX", getBtnGenereazaNOTAAUTORIZARE());
                        //PortletResponseUtil.sendFile(portletRequest, (MimeResponse) portletResponse, fisierNume+".xlsx", is, "xlsx");
                        is.close();
                        
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }   
                }
                
            }
            
            
        });

我的 YesNoConfirmation 类的代码是:

public class YesNoConfirm extends Window{
    
    public boolean isConfirmed = false;
    public void openConfirmationPanel(String confirmationTitle) {
        // Create a sub-window and set the content
        Window subWindow = new Window(confirmationTitle);
        VerticalLayout subContent = new VerticalLayout();
        subContent.setMargin(true);
        subWindow.setContent(subContent);
        
        HorizontalLayout buttonStack = new HorizontalLayout();
        Button okButton = new Button("OK");
        Button cancelButton = new Button("Anuleaza");
        buttonStack.addComponent(okButton);
        buttonStack.addComponent(cancelButton);
        
        // Put some components in it
        subContent.addComponent(new Label("Confirmare"));
        subContent.addComponent(buttonStack);
        
        // Center it in the browser window
        subWindow.center();
        
        // Open it in the UI
        UI.getCurrent().addWindow(subWindow);
        
        okButton.addClickListener(ClickEvent -> {
            this.isConfirmed = true;
        });
        
        cancelButton.addClickListener(ClickEvent -> {
            this.isConfirmed = false;
        });
        
    }
    
    public boolean getConfirmationStatus() {
        return this.isConfirmed;
    }
}

提前致谢

Java Vaadin 渲染 服务器端 vaadin8

评论


答:

4赞 Erik Lumme 8/16/2021 #1

面板代码未阻塞。看看下面的两行

panel1.openConfirmationPanel("Confirmare");
if(panel1.getConfirmationStatus() == true) {

打开面板后,将立即执行下一行。此时,状态仍然是 ,因此它不会执行 if 语句中的代码。false

即使稍后更新了该标志,该代码也不会再次执行,因此不会发生任何事情。isConfirmed

您可以做的是使用回调。您可以将按钮单击侦听器直接传递给 ,也可以使用其他类,例如自定义类/接口。YesNoConfirmRunnable

例如,像这样的东西:

public class YesNoConfirm extends Window {

    private final Runnable runOnYesAction;

    public YesNoConfirm(Runnable runOnYesAction) {
        this.runOnYesAction = runOnYesAction;
    }
    
    public void openConfirmationPanel(String confirmationTitle) {
        // Create the window etc
        ...
    
        // Open it in the UI
        UI.getCurrent().addWindow(subWindow);
        
        okButton.addClickListener(clickEvent -> runOnYesAction.run());
        
        cancelButton.addClickListener(clickEvent -> {
            // Maybe just close the window?
        });
        
    }
}

你会像这样使用它:

YesNoConfirm confirmDialog = new YesNoConfirm(() -> {
    // Put code that should be run if Yes is clicked here
});

评论

1赞 Anna Koskinen 8/16/2021
@Cristis 请将答案标记为已接受,以表明问题已解决:)