简体   繁体   中英

SWT - MessageBox opening and closing

Is it possible to open and close a messagedialog without the user having to click buttons?

When the user clicks a button on my form, the action from that button goes server side and gathers a list of items, takes a couple of seconds. I want a way to tell the users that the action is in progress. I was thinking a messagedialog with some text. .

Opens the message

MessageDialog.openInformation(shell, "Information", "Getting List From Server"); 

Then some how closes it (something like MessageDialog.close )?

I looked at a progress bar but that was more than I really needed.

It might look like a big overhead at first, but I would suggest using an IProgressMonitor which shows the progress of your task.

The user will know whats going on when he/she sees a progress bar, rather than a dialog that looks like the gui is frozen.

Here is an article by Eclipse on how to use progress monitors correctly.

If you really want to go for your idea (which I would not suggest), you can try the following:

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();
    }

}

This however, will not block your remaining gui, so the user will be able to use it in the meantime.

EDIT :

Just found out, that Opal has something called an InfiniteProgressPanel , which might be something for you. Have a look...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM