是否可以在用户无需单击按钮的情况下打开和关闭消息对话框?
当用户单击我的表单上的一个按钮时,该按钮的操作会转到服务器端并收集项目列表,这需要几秒钟的时间。我想要一种方法来告诉用户操作正在进行中。我在想一个带有一些文本的消息对话框。 .
打开消息
MessageDialog.openInformation(shell, "Information", "Getting List From Server");
然后一些如何关闭它
(类似于 MessageDialog.close
)?
我查看了进度条,但这超出了我真正需要的范围。
最佳答案
一开始看起来开销很大,但我建议使用 IProgressMonitor
这显示了您的任务进度。
当用户看到一个进度条而不是一个看起来像 gui 被卡住的对话框时,他/她就会知道发生了什么。
Here是 Eclipse 的一篇关于如何正确使用进度监视器的文章。
如果你真的想实现你的想法(我不建议这样做),你可以尝试以下方法:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
BazMessageDialog dialog = new BazMessageDialog(shell, "Information", null, "Getting List From Server", MessageDialog.INFORMATION, new String[]{"OK", "Cancel"}, 0);
dialog.open();
/* Do your stuff */
dialog.reallyClose();
shell.dispose();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static class BazMessageDialog extends MessageDialog
{
public BazMessageDialog(Shell parentShell, String dialogTitle,
Image dialogTitleImage, String dialogMessage,
int dialogImageType, String[] dialogButtonLabels,
int defaultIndex) {
super(parentShell, dialogTitle, dialogTitleImage, dialogMessage,
dialogImageType, dialogButtonLabels, defaultIndex);
setBlockOnOpen(false);
}
public void reallyClose()
{
cancelPressed();
}
}
但是,这不会阻止您剩余的图形用户界面,因此用户可以同时使用它。
编辑:
刚刚发现,Opal有一个叫做 InfiniteProgressPanel 的东西,这可能适合您。看看...
关于java - SWT - MessageBox 打开和关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12375469/