非阻塞模态Swing进度对话框 [英] Non-blocking modal Swing progress dialog

查看:82
本文介绍了非阻塞模态Swing进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个愚蠢的问题,但是我真的无法解决这个问题:我在Swing应用程序中有一些长时间运行的过程,这可能需要几分钟.我想在此过程进行中向用户显示进度对话框.我还希望阻止用户执行进一步的操作,例如在过程进行时按下按钮.

如果我在EDT上执行该过程,则很显然,这会阻止最终用户在过程进行时执行任何操作.但是,由于EDT忙于处理,所以它永远不会渲染对话框的内容,所以我只得到对话框的轮廓.

但是,如果我在辅助线程中执行该过程,则最终用户可以单击按钮,并使EDT操纵该工作者也在同时操纵的状态,并带有坏东西作为结果.如果我将对话框设置为模态对话框,则不会发生这种情况,但是Swing中的 modal 语句也暗示 blocking ,因此,当我调用setVisible(true)时,线程将永远处于阻塞状态.

所以我当前的解决方案"是使用工作线程,然后产生一个单独的线程,其唯一目的是吸收setVisible的阻塞性质.当然,必须有一个更好的解决方案!

那么我如何获得一个对话框,以防止用户与系统的其余部分(模态)进行交互,但不阻止导致其可见的线程?

解决方案

您应该使用GlassPane阻止用户进行交互.好处是,这是针对用户而不是您的程序的模式,因此不会阻塞您的线程.

好消息是,您还可以将其用于出色的GUI效果,例如使GUI部分忙于着色并且用户无法与之交互.您还可以在GlassPane中显示繁忙的gif图像.

链接:-

  1. 关于GlassPane的官方教程
  2. 一些有用的教程
  3. 另一个tu
  4. Oreilly的Swing Hacks 很棒. li>

A daft question, but I really cannot get this to work: I have some long-running process in a Swing application which may take several minutes. I want to display a progress dialog to the user while this process is ongoing. I also want to prevent the user from performing further actions, eg pressing a button while the process is going on.

If I do the process on the EDT, this obviously prevents the end-user from doing anything while the process is going on. But because the EDT is busy processing, it never gets around to rendering the dialog's content, so I just get the outline of the dialog.

But if I do the process in a worker thread, the end-user can click buttons and cause the EDT to manipulate state that the worker is also manipulating at the same time, with bad stuff as a consequence. If I make the dialog modal, that does not happen, but modal in Swing also implies blocking, so the moment I call setVisible(true), the thread blocks forever.

So my current "solution" is to use a worker thread, and then spawn off a separate thread whose only purpose is to absorb the blocking nature of setVisible. Surely there must be a better solution!

So how do I get a dialog that prevents the user from interacting with the rest of the system (modal) but does not block the thread that causes it to be visible?

解决方案

You should be using the GlassPane from preventing the user from interacting. The good thing is this is modal for the user and not your program so your thread won't get blocked.

The great news is that you can also use it for cool GUI effects like shading the GUI portion busy and the user can't interact with. You can also display a busy gif image in the GlassPane.

Links:-

  1. Official tutorial on GlassPane
  2. Some helpful tutorials
  3. Another tut
  4. Oreilly's Swing Hacks is excellent.

这篇关于非阻塞模态Swing进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆