我有一个按钮,应该生成一个Excel文件与一些数据。我需要添加一个弹出窗口,询问"你确定吗?"有两个按钮:"是"或"NO".
我有一个按钮,我添加了一个clicklistener,在clicklistener中,我已经从一个类(YesNoConfirmation)初始化了一个对象,我已经创建了。
YesNoConfirmation创建一个带有字符串和2个按钮的窗口,并将其添加到主窗口。我在确认窗口的2个按钮上添加了clicklisteners。我的YesNoConfirmation类有一个名为"isConfirmed"的成员,它应该保留状态——无论是否确认。来自YES/NO按钮的2个clicklistener更新类的isConfirmed成员。然后,根据"isConfirmed"的值,它应该决定是否执行生成Excel文件的代码。我做了一些调试,似乎是"isConfirmed"成员得到相应的更新,但是当我检查用户是否点击了YES或NO与this: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;
}
}
Thanks in advance
面板代码未阻塞。看看下面的两行
panel1.openConfirmationPanel("Confirmare");
if(panel1.getConfirmationStatus() == true) {
打开面板后,立即执行下一行。此时,状态仍然是false
,因此它不执行if语句中的代码。
即使稍后更新了isConfirmed
标志,该代码也不会再次执行,因此不会发生任何事情。
你能做的是使用回调。您可以将按钮单击侦听器直接传递给YesNoConfirm
,也可以使用其他类,如Runnable
或自定义类/接口。
例如:
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
});