java - 禁用父框架而不停止线程?

标签 java multithreading swing user-interface modal-dialog

我正在开发一个 Swing 应用程序,我需要在后台运行无限循环(运行直到:1)选择 JDialog 的取消按钮或 2)找到它正在搜索的输入数据)而模态对话框显示不确定的进度条。

我注意到,如果 JDialog 是模态的,那么 SwingWorker 将不会执行其任务,直到 JDialog 关闭(并在 EDT 上释放其 DeathGrip,我猜......?)。如果 JDialog 不是模态的,那么 SwingWorker 的任务将在后台愉快地执行。

我一直在做一些研究,但我不是线程/EDT 专家,并且很难找出原因/解决方案。

任何有关这种情况/线程/EDT/SwingWorker 的意见或建议的解决方案,我们将不胜感激。

(问题直接来自:http://www.coderanch.com/t/346275/GUI/java/SwingWorker-Modal-JDialogs)

我尝试了有关 JDialog 的 setVisible 调用的解决方案,就像该用户发现的解决方案一样,但我仍然无法同时执行两个线程。任何帮助将不胜感激。

相关:

public Dialog(JFrame parentFrame, String equipmentName) {
    super(parentFrame, "Progress");

    this.hasRequestedCancel = false;
    this.equipmentName = equipmentName;

    add(createMainPanel());
    setIconImage(Toolkit.getDefaultToolkit().getImage(SomeClass.class.getResource(ICON_PATH)));
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    setModalityType(ModalityType.DOCUMENT_MODAL);
    pack();
    setSize(550, 100);
    setResizable(false);
    setLocationRelativeTo(parentFrame);
    setVisible(true);
}

还有

SwingWorker<File, Void> worker = createSwingWorker(params, ...);
worker.execute();

private SwingWorker<File, Void> createSwingWorker(final File someFile, final SomeClass asdf, final String param3) throws IOException {
    SwingWorker<File, Void> swingWorker = new SwingWorker<File, Void>() {

        @Override
        protected File doInBackground() throws IOException {
            Dialog progressBar = new Dialog(SomeClass.this, SomeClass.this.equipManufacturerDevice);

            try {
                while(!someFile.exists() && !progressBar.hasRequestedCancel()) {
                    Thread.sleep(SomeClass.SLEEP_DURATION);
                    System.out.println("yo");
                }
            } catch (InterruptedException e) {
            }

            ...
        }

        @Override
        protected void done() {
            ...
        }
    };

    return swingWorker;
}

最佳答案

问题是您在 Dialog 的构造函数中调用 setVisible(true) ,无论如何,这是一种不鼓励的做法(您刚刚找到了一个原因,为什么)。

将对话框的创建和打开分开,就不再有这个问题了。以下示例代码演示了如何实现这一点:

final Dialog d=new Dialog((Window)null);
d.setSize(300, 300);
d.setModal(true);
new SwingWorker<Object,Object>() {
  @Override
  protected Object doInBackground() throws Exception {
    System.out.println("long running stuff");
    TimeUnit.SECONDS.sleep(10);
    System.out.println("end of long running stuff");
    return null;
  }
  @Override
  protected void done() {
    d.dispose();
  }
}.execute();
System.out.println("before setVisible(true)");
d.setVisible(true);// will block
System.out.println("after setVisible(true)");

关于java - 禁用父框架而不停止线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429087/

相关文章:

java - 获取 FinalizedDeleatedExecutorService 的状态

c# - 从终端分离时如何为单声道制作 cpu 友好的无限循环

multithreading - 为什么名称为 "monitor"?

Java Swing : Set member which is displayed in JListBox

java - 想要实现 jtattoo 的外观和感觉,但不起作用

java - Spring Boot 3.0.0 + Spring Cloud 2021.0.5 未定义负载平衡的 Feign 客户端。您是否忘记包含 spring-cloud-starter-loadbalancer ?

java - 为什么我不能正确交换?

java - 如何使用 key 监听器来验证对文本字段的输入

java - 单击 onOptionsItemSelected 中的 AlertDialog 中的按钮会使模拟器崩溃

java - 如何更改websphere监听端口