[英]Prevent Swing GUI from becoming unresponsive when invoking a method which is both accessing Swing components and is time-consuming
The following line:以下行:
SwingUtilities.updateComponentTreeUI(aComponent);
is making my GUI unresponsive.使我的 GUI 无响应。
When invoking this method to update the laf on a large portion of a GUI, it takes a lot of time, and so makes the GUI unresponsive during this operation.当调用此方法更新大部分 GUI 上的 laf 时,会花费大量时间,因此在此操作期间会使 GUI 无响应。
Since this operation is manipulating the GUI, one can't use a SwingWorker for this either.由于此操作正在操作 GUI,因此也不能为此使用 SwingWorker。 From the SwingWorker documentation:
从 SwingWorker 文档:
Time-consuming tasks should not be run on the Event Dispatch Thread.
不应在事件调度线程上运行耗时的任务。 Otherwise the application becomes unresponsive.
否则应用程序变得无响应。
Swing components should be accessed on the Event Dispatch Thread only.
Swing 组件只能在事件调度线程上访问。
The problem here though, is that the operation is accessing Swing components and is time-consuming.但是,这里的问题是该操作正在访问 Swing 组件并且非常耗时。
Is there a good way to prevent this unresponsiveness?有没有防止这种反应迟钝的好方法?
Since what you do is changing the L&F, and that might severely impact the appearance and usability of the GUI, ask yourself whether you really want the application to be responsive during that time.由于您所做的是更改 L&F,这可能会严重影响 GUI 的外观和可用性,因此请问问自己是否真的希望应用程序在此期间响应。 It may be better to display a message ("Please wait..." or something) using the glass pane , and freeze the GUI while the L&F is updated.
使用玻璃窗格显示消息(“请稍候...”或其他内容)可能会更好,并在 L&F 更新时冻结 GUI。
Now, as others have suggested, you may want to investigate why updating the component tree is so slow.现在,正如其他人所建议的那样,您可能想要调查为什么更新组件树如此缓慢。
I was going to suggest looking through your GUI to identify any custom or third-party components that contained a lot of sub-components or that had any unusual or inefficient method to revalidate itself.我将建议查看您的 GUI 以识别任何自定义或第三方组件,这些组件包含大量子组件,或者具有任何不寻常或低效的方法来重新验证自身。 It looks like that was the case, as you mentioned the date picker was a serious bottleneck.
看起来情况就是这样,正如您提到的日期选择器是一个严重的瓶颈。
You suggested splitting the calls to updateComponentTree
into several sub-tasks that allow events to occur in between, which might be a 'hack' but isn't too bad, unless the change of font changes the sizes of elements and may cause the user to miss a button etc.您建议将对
updateComponentTree
的调用拆分为几个允许事件在其间发生的子任务,这可能是一个“黑客”,但也不算太糟糕,除非字体的更改会改变元素的大小并可能导致用户错过一个按钮等。
If possible, I would suggest looking at the code in the date picker component and see if you can rewrite it so that instead of hiding the components in the pop-up, it actually removes/disposes them, and recreates them when necessary.如果可能的话,我建议查看日期选择器组件中的代码,看看是否可以重写它,这样它就不会在弹出窗口中隐藏组件,而是实际删除/处置它们,并在必要时重新创建它们。 This should have no noticeable effect on the responsiveness of the date picker when it's being used, but will certainly speed up component tree updates when the pop-up is not visible.
这应该对日期选择器在使用时的响应没有明显影响,但是当弹出窗口不可见时肯定会加快组件树的更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.