简体   繁体   English

在Mac上无法使用选定控件上的Java SWT更改标签文本

[英]Java SWT Changing Label text on widget selected is not working on Mac

In my java swt application I have following code. 在我的java swt应用程序中,我有以下代码。 On button select I need to change text of the label twice one is before running thread and other one is after thread finishes. 在按钮选择上,我需要两次更改标签的文本,一个是在运行线程之前,另一个是在线程完成之后。 It works on windows but on Mac it does not display the first text. 它可以在Windows上运行,但在Mac上则不显示第一个文本。 Why this does not work on Mac? 为什么这在Mac上不起作用?

button.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent arg0) {
            statusLabel.setText("Running...");
            Thread background = new Thread() {
               @Override
               public void run() {
               // Long running task
               }
            };
            background.start();
            try {
                background.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            statusLabel.setText("Finished");
        }
    });

The call to Thread.join is blocking the UI thread which will cause it to stop responding. Thread.join的调用阻塞了UI线程,这将导致它停止响应。 Exactly how much gets updated before this happens depends on the details on the SWT implementation on each platform. 在此之前,究竟需要多少更新,取决于每个平台上SWT实现的细节。

You should update the UI from the background thread once the code has finished. 代码完成后,您应该从后台线程更新UI。

Something like: 就像是:

button.addSelectionListener(new SelectionAdapter() {

   public void widgetSelected(SelectionEvent arg0) {

      statusLabel.setText("Running...");

      Thread background = new Thread() {
         @Override
         public void run() {
            // Long running task

            // Update UI from background thread
            Display.getDefault().asyncExec(() -> statusLabel.setText("Finished"));
         }
      };

      background.start();
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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