Swing Worker无法在模式Jdialog中更新GUI组件

时间:2019-02-02 19:13:53

标签: java multithreading swing swingworker

我有一个带有进度条和文本区域的模式Jdialog。我正在启动Swing Worker,以执行一些后台任务,并使用发布过程更新jdialog中的文本区域。但是,在运行程序时,我观察到即使调用了process方法,它仍然不会更新jdialog中的文本区域。另外,请注意,我关闭的JDialog在摆动工人的做好方法,它工作正常。谁能告诉我为什么(SwingWorker)处理方法中的gui更新没有发生?

JDialog类的 -

import { ActivatedRoute } from '@angular/router';

@Component({
selector: 'foo',
template: '<input type="text" [value]="admissionNo">'
...
})
export class FooComponent implements OnInit {
  admissionNo: string;
  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.route.queryParams
      .subscribe(params => this.admissionNo = params.admission_no || '');
  }
}

}

SwingWorker类:​​

public class ProgressDialog extends JDialog {

private static final long serialVersionUID = 1L;
private GridBagLayout gridBag;
private GridBagConstraints constraints;
private ProgressDialog.ProgressBar progressBar;
private JScrollPane scrollPane;
private JTextArea textArea;

ProgressDialog(Frame owner, String title, boolean modal, int numTasks) {

    super(owner,title,modal);

    gridBag = new GridBagLayout();
    constraints = new GridBagConstraints();

    this.progressBar = new ProgressDialog.ProgressBar();
    this.progressBar.init(numTasks);

    this.textArea = new JTextArea(10,30);
    this.textArea.setEditable(false);
    this.scrollPane = new JScrollPane(this.textArea);
    this.scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    this.scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);

    this.setLayout(gridBag);

    constraints.gridx = 0;
    constraints.gridy = 0;
    gridBag.setConstraints(progressBar, constraints);

    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.insets = new Insets(10,0,10,0);
    gridBag.setConstraints(scrollPane, constraints);


    this.add(progressBar);
    this.add(scrollPane);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    this.setSize(400, 300);
    this.setLocationRelativeTo(null);
}

class ProgressBar extends JProgressBar {

    ProgressBar() {

    }

    void init(int numTasks) {
        setMaximum(numTasks);
        setStringPainted(true);
        setValue(0);
    }

    void setProgress(int taskCompleted) {
        int totalTasks = getMaximum();
        setValue(taskCompleted);
        setString(taskCompleted+"/"+totalTasks);
    }
}

static long getSerialversionuid() {
    return serialVersionUID;
}

GridBagLayout getGridBag() {
    return gridBag;
}

GridBagConstraints getConstraints() {
    return constraints;
}

ProgressDialog.ProgressBar getProgressBar() {
    return progressBar;
}

JScrollPane getScrollPane() {
    return scrollPane;
}

JTextArea getTextArea() {
    return textArea;
}

}

调用Jdialog和Swingworker的控制器代码-

public class SwingWorkers {

static class ConnPool extends SwingWorker<Void, TaskType<String>>{

    private ProgressDialog pDialog;

    ConnPool(ProgressDialog pDialog) {
        this.pDialog = pDialog;
    }

    protected Void doInBackground() throws Exception {
        Runner<String> runner = new Runner<>();

        Future<TaskType<String>> fut = runner.run(new Tasks.InitResources());
        runner.shutDown();

        while(!fut.isDone()) {
            Thread.sleep(1000);
        }

        publish(fut.get());

        return null;
    }

    protected void process(List<TaskType<String>> results) {
        if(results.size() > 0) {
            TaskType<String> lastResult = results.get(results.size()-1);
            pDialog.getTextArea().append(lastResult.getTaskStatusMesg());
            pDialog.getTextArea().append("\n");
            pDialog.getProgressBar().setValue(results.size());
        }
    }

    protected void done() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        pDialog.dispose();
    }

}

我在某处读到,一旦使模式Jdialog可见,EDT将被阻止直到再次关闭?但是,这仍然没有解释为什么的SwingWorker done方法是能够关闭的JDialog。

编辑: 我知道我只调用一次处理方法(应该在while循环中)。我打算用发布流程机制,更耗时的任务后,我能得到这个场景(从工艺方法GUI更新)工作。

还要注意在done方法,睡眠仅用于测试。我可以在完成方法中不使用睡眠方法来重现相同的问题。

2 个答案:

答案 0 :(得分:3)

一个巨大的问题就在这里

protected void done() {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    pDialog.dispose();
}

在工人的done()方法被调用的EDT或事件分派线程,你知道当你睡觉的这个线程会发生什么 - 整个GUI进入睡眠状态,变得完全没有反应。如果你想拖延处置的对话,并保持你的GUI功能,不这样做,事实上, 从不拨打Thread.sleep在EDT。

选项1:

相反,在大多数其他所有Swing延迟策略中,请使用Swing计时器,例如,类似这样的代码(未经测试):

protected void done() {
    int timerDelay = 5000;
    new Timer(timerDelay, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            pDialog.dispose();
            ((Timer) e.getSource).stop();
        }
    }).start();
}

如果您不熟悉Swing计时器,请查看Swing Timer Tutorial

选项2:

Thread.sleep(5000)放在doInBackground()方法的末尾,就在return null;之前


另一个问题:您使用的是publish / process方法对,但只有 一次 违反了其目的。也许您打算在轮询while循环内调用发布?当然,您只能调用一次get(),但是还有另一种方法可以从正在运行的进程中提取信息吗?如果是这样,则需要间歇地获取信息,或者在轮询while循环内,或者通过任何允许您提取中期结果的过程。

例如:

import java.awt.BorderLayout;
import java.awt.Dialog.ModalityType;
import java.awt.Dimension;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.beans.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import javax.swing.*;


public class TestProgressDialog extends JPanel {
    private static final long serialVersionUID = 1L;
    private ProgressDialog pDialog;
    private JSpinner taskNumberSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 20, 1));

    public TestProgressDialog() {
        setPreferredSize(new Dimension(800, 650));
        add(new JButton(new AbstractAction("Launch Dialog") {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                Window owner = SwingUtilities.windowForComponent(TestProgressDialog.this);
                String title = "Dialog";
                ModalityType modal = ModalityType.MODELESS;
                int numTasks = (int) taskNumberSpinner.getValue();
                pDialog = new ProgressDialog(owner, title, modal, numTasks);
                pDialog.pack();
                pDialog.setLocationByPlatform(true);
                pDialog.append("Initializing background tasks\n");
                MyWorker myWorker = new MyWorker(numTasks, pDialog);
                myWorker.addPropertyChangeListener(new WorkerListener(pDialog));
                myWorker.execute();
                pDialog.setVisible(true);
            }
        }));
        add(new JLabel("Number of Tasks:"));
        add(taskNumberSpinner);
    }

    private static void createAndShowGui() {
        TestProgressDialog mainPanel = new TestProgressDialog();

        JFrame frame = new JFrame("Test Progress Dialog");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

interface Progressable {
    void setProgress(int progress);
    void append(String text);
}

class ProgressDialog extends JDialog implements Progressable {
    private static final long serialVersionUID = 1L;
    private JProgressBar progressBar = new JProgressBar(0, 100);
    private JTextArea textArea = new JTextArea(10, 30);
    private JScrollPane scrollPane = new JScrollPane(textArea);

    ProgressDialog(Window owner, String title, ModalityType modal, int nunTasks) {
        super(owner, title, modal);
        progressBar.setStringPainted(true);

        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(progressBar, BorderLayout.PAGE_START);
        add(scrollPane);
    }

    @Override
    public void append(String text) {
        textArea.append(text);
    }

    @Override
    public void setProgress(int progress) {
        progressBar.setValue(progress);
    }

}

class MyCallable implements Callable<String> {
    private static final long MAX_DUR = 6 * 1000;
    private static final long MIN_DUR = 1000;
    private String text;

    public MyCallable(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    @Override
    public String call() throws Exception {
        // just wait some random delay and then return a String
        long timeout = (long) (Math.random() * MAX_DUR + MIN_DUR);
        TimeUnit.MILLISECONDS.sleep(timeout);
        return text + " time out: " + timeout;
    }

}

class WorkerListener implements PropertyChangeListener {
    private Progressable progressable;

    public WorkerListener(Progressable progressable) {
        this.progressable = progressable;
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName() != null && evt.getPropertyName().equals("progress")) { 
            int progress = (int)evt.getNewValue();
            progressable.setProgress(progress);
        }
    }
}

class MyWorker extends SwingWorker<Void, String> {

    private Progressable progressable;
    private List<Future<String>> futures = new ArrayList<>();
    private CompletionService<String> completionService;
    private int numTasks;
    // private BlockingQueue<Future<String>> completionQueue;


    public MyWorker(int numTasks, Progressable progressable) {
        this.numTasks = numTasks;
        this.progressable = progressable;
        ExecutorService service = Executors.newFixedThreadPool(numTasks);
        completionService = new ExecutorCompletionService<>(service);

        for (int i = 0; i < numTasks; i++) {
            futures.add(completionService.submit(new MyCallable("My Callable " + i)));
        }
        service.shutdown();
    }

    @Override
    protected Void doInBackground() throws Exception {
        while (futures.size() > 0) {
            Future<String> future = completionService.take();
            futures.remove(future);
            int progress = (100 * (numTasks - futures.size())) / numTasks;
            progress = Math.min(100, progress);
            progress = Math.max(0, progress);
            setProgress(progress);
            if (future != null) {
                publish(future.get());
            }
        }
        return null;
    }

    @Override
    protected void process(List<String> chunks) {
        for (String chunk : chunks) {
            progressable.append(chunk + "\n");
        }
    }

    public Progressable getpDialog() {
        return progressable;
    }

}

答案 1 :(得分:0)

这里的问题是您的Jdialog框是模态的。每当弹出窗口(或JDialog)为setModal(true)时,EDT线程就会被阻止。这样,当使用模式true弹出时,用户将无法执行任何操作。

您必须将其设置为setmodal(false),然后在弹出窗口中更新内容,然后再次将其设置为setmodal(true)