非阻塞模态 Swing 进度对话框

发布于 2024-09-26 17:59:15 字数 507 浏览 9 评论 0原文

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

如果我在 EDT 上执行该过程,这显然会阻止最终用户在该过程进行时执行任何操作。但由于 EDT 正忙于处理,它永远不会抽出时间来渲染对话框的内容,因此我只获取对话框的轮廓。

但是,如果我在工作线程中执行该过程,最终用户可以单击按钮并导致 EDT 操作工作线程同时也在操作的状态,从而导致坏东西。如果我将对话框设置为模态,则不会发生这种情况,但是 Swing 中的模态也意味着阻塞,因此当我调用 setVisible(true) 时,线程永远阻塞。

因此,我当前的“解决方案”是使用工作线程,然后生成一个单独的线程,其唯一目的是吸收 setVisible 的阻塞性质。当然必须有更好的解决方案!

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-10-03 17:59:16

您应该使用 GlassPane 来阻止用户交互。好处是这对于用户而不是您的程序来说是模态的,因此您的线程不会被阻塞。

好消息是,您还可以使用它来实现很酷的 GUI 效果,例如对繁忙且用户无法交互的 GUI 部分进行阴影处理。您还可以在 GlassPane 中显示繁忙的 gif 图像。

链接:-

  1. GlassPane 官方教程
  2. 一些有用的教程
  3. 另一张图
  4. Oreilly 的 Swing Hacks 非常棒。

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.
诗笺 2024-10-03 17:59:16

你一定做错了什么。尝试如何使用进度条教程。如果您可以理解该代码的工作原理,它应该为您提供实现所需功能的方向。

请注意,进度条通常是模态的。

You must be doing something wrong. Try the tutorial on How to Use Progress Bars. If you can understand how that code works, it should give you the direction for achieving the required feature.

Note that progress bars are usually modal.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文