如何指示 JComboBox 正在加载值?
我有一个 JComboBox,其值是通过网络检索的。
我正在寻找一种方法来向用户表明这一事实,当用户想要查看列表时,展开下拉列表,并且只有才会检索数据。
基本要求包括:
- JComboBox 的下拉列表不应锁定 EDT,但组合的操作不应在有值之前起作用。
- 用户应该知道所有数据何时被检索。
- 指示的大小(UI 空间)应尽可能小。
请注意,直到用户想要查看组合的值(即展开下拉列表)时才会检索数据。
我使用的解决方案:
我使用了 SwingWorker
来保持 UI 响应。该组合框使用 JIDE 的 Overlayable 和 JIDE 的 InfiniteProgressPanel 进行叠加,后者监听工作线程。
I have a JComboBox whose values are retrieved across the net.
I'm looking for a way to indicate that fact to the user, when the user wants to see the list, expands the drop down, and only then the data is being retrieved.
The basic requirements include:
- JComboBox's drop-down shouldn't lock the EDT, but the combo's action should not work until there are values.
- User should know when all data has been retrieved.
- The size (UI real-estate) of the indication should be as small as possible.
Note that the data isn't retrieved until the user wants to see the combo's values (i.e. expands the drop-down list).
The solution i've used:
I've used a SwingWorker
to keep the UI responsive. The combo box was overlayed using JIDE's Overlayable
with JIDE's InfiniteProgressPanel
that listens to the worker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免锁定 EDT,您的数据检索应该在后台线程中完成。我会使用 SwingWorker 来查找并加载值,因为这可以提供后台线程以及其他使其对 Swing 非常友好的功能。我将使 JComboBox 启用属性
false
直到加载所有值,然后通过setEnabled(true)
启用它。您将知道 SwingWorker 已通过其done()
方法(通过重写它)完成,或者通过向 SwingWorker 添加 PropertyChangeListener 并在其状态为SwingWorker.StateValue.DONE 时收到通知来完成
。用户知道该过程已完成的一种方法是他们将看到组合框何时重新启用。如果您想要更明显的指示器,您可以显示 JProgressBar 或 ProgressMonitor。如果您希望 GUI 外观基本保持不变,则可以将其显示在对话框中。
To avoid locking the EDT, your data retrieval should be done in a background thread. I would use a SwingWorker to find and load the values since this makes available a background thread with other goodies that make it very Swing-friendly. I would make the JComboBox enabled property
false
until all values have been loaded, and then enable it viasetEnabled(true)
. You will know the SwingWorker is done either through itsdone()
method (by overriding it), or by adding a PropertyChangeListener to the SwingWorker and being notified when its state isSwingWorker.StateValue.DONE
.One way for the user to know that the process is complete is that they will see when the combo box has been re-enabled. If you want a more obvious indicator, you could display a JProgressBar or a ProgressMonitor. This could be displayed in a dialog if you wish to leave the GUI appearance mostly unchanged.
我通过添加“正在加载...”项和 JComboBox 周围的特殊边框来实现它。单击时,将启动单独的线程,通过 SwingUtilities.invokeAndWait 添加新项目。加载完成后,“正在加载...”最后一项将被删除。
I implemented it by adding "Loading..." item and a special border around the JComboBox. On click separate thread is started adding new items via SwingUtilities.invokeAndWait. When loading is completed the "Loading..." last item is removed.
为了不强迫我的用户等待数据加载,请结合 eel 和 stan 的答案:-)
to not force my users to wait until the data is loaded, combine the answers by eel and stan :-)