开发者

SWT Force Re-Draw of Label

I have a simple method that sets the text of a label:

public void setStatus(final String status)
{
   statusLabel.setText(status);
}

When I call it before any sort of Display.asyncExec(Runnable), it seems to not execute until after that Runnable has completed. I have even tried to implement it in the Runnable, and it doesn't work. For example, I have a开发者_运维技巧 'load file' menu item that I would like the status to display "Loading: [filename]" after a user selects a file:

    MenuItem mntmLoadFile = new MenuItem(menu_4, SWT.NONE);
    mntmLoadFile.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent arg0) {
            String file = GuiWorkshop.selectFile(shell, new String[]{"*.apk"});
            if(file != null){
                try{
                    setStatus("Loading File: " + file);
                    controller.loadFile(new File(file));
                } catch (Exception e){
                    GuiWorkshop.messageError(shell, "Could not load file: " + e.getMessage());
                    logger.error("Could not load file: ", e);
                }
                clearStatus();
            }
        }
    });
    mntmLoadFile.setText("Load file");

The line controller.loadFile(new File(file)); is what performs all of the loading, but the setStatus will never update until it has completed. Is there a way to force that to finish, prior to the next line executing? I am not sure if this is a local thread problem, or an SWT thread problem.


You can call Display.readAndDispatch() in a loop until it returns false. This will ensure all redraw operations have completed.

...
setStatus("Loading File: " + file);

while (display.readAndDispatch())
{
   // nothing to do
}

controller.loadFile(new File(file));
...

Note: You probably do not want to call controller.loadFile() on the UI thread. This will cause the UI to hang. Instead, you should call controller.loadFile() in a background thread. When the background thread finishes, have the UI thread update the status.


You have to refresh the label after setting the text. The Control API (which is the superclass for all SWT components) has a method redraw(), which, calls the create contents method again and should provide the result you are looking for.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜