我正在使用Vaadin MessageBox
,我所遇到的问题是,MessageBox
之后的代码会立即运行,并且不会等待MessageBox
回答。我试图将我的代码打包在一个返回布尔值的函数中。但似乎不可能用MessageBox()
来做,这里是我想要做的一个示例:
private boolean Delete(){
return true;
}
private boolean IsDone(){
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> return Delete()) //here is where i have problem
.withNoButton(()-> {})
.open();
}
我也试过了,
.withYesButton(()-> {return Delete();})
它看起来像这样,但没有return true;
.withYesButton(()-> {if(Delete){
return true; //doesn't work!
System.out.printLn("Works!"); // works :-?
}})
任何人都知道这种情况或有任何想法吗?
答案 0 :(得分:1)
需要用户交互的操作不是“停止”代码执行。您需要组织代码,以便在处理程序中继续执行yes和no按钮。因此,请确保您的“流程”停止显示确认框,并在“是”或“否”之后继续。
private void IsDone(){
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> onUserAnsweredYes())
.withNoButton(()-> onUserAnsweredNo())
.open();
}
答案 1 :(得分:0)
当用户按下是按钮时,withYesButton
方法将获得Runnable
。让我们看一下Runnable
的实施:
public interface Runnable {
void run();
}
如您所见,run
会返回void
,因此您无法返回任何值,例如true
。相反,您必须提供用于删除所需内容的代码,例如:
MessageBox
.createQuestion()
.withCaption("Delete")
.withMessage("Are you sure?")
.withYesButton(()-> codeToDeleteSomething())
.open();