如何指示 JComboBox 正在加载值?

发布于 2024-12-23 01:58:34 字数 416 浏览 1 评论 0原文

我有一个 JComboBox,其值是通过网络检索的。

我正在寻找一种方法来向用户表明这一事实,当用户想要查看列表时,展开下拉列表,并且只有才会检索数据。

基本要求包括:

  1. JComboBox 的下拉列表不应锁定 EDT,但组合的操作不应在有值之前起作用。
  2. 用户应该知道所有数据何时被检索。
  3. 指示的大小(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:

  1. JComboBox's drop-down shouldn't lock the EDT, but the combo's action should not work until there are values.
  2. User should know when all data has been retrieved.
  3. 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 技术交流群。

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

发布评论

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

评论(3

如此安好 2024-12-30 01:58:34

为了避免锁定 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 via setEnabled(true). You will know the SwingWorker is done either through its done() method (by overriding it), or by adding a PropertyChangeListener to the SwingWorker and being notified when its state is SwingWorker.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.

岁吢 2024-12-30 01:58:34

为了不强迫我的用户等待数据加载,请结合 eel 和 stan 的答案:-)

  • 从包含零或一个实际值的模型开始,加上虚拟条目“正在加载”
  • ,注册一个 PopupMenuListener 并启动一个 SwingWorker 加载数据(到一个单独的数据结构,可能是一个新模型)在加载时在其第一个菜单中将变得可见
  • ,选择虚拟条目(和/或其他任何适合通知​​用户什么的内容)发生),该操作必须知道“还没有做什么”,并
  • 在收到 DONE 时将数据替换/填充到组合的模型中,并听取工作人员的意见

to not force my users to wait until the data is loaded, combine the answers by eel and stan :-)

  • start off with the model containing zero or one real value plus the dummy entry "loading"
  • register a PopupMenuListener and start a SwingWorker loading the data (into a separate datastructure, might be a new model) in its very first menuWillBecomeVisible
  • while loading, select the dummy entry (and/or whatever else is appropriate to inform the user what's happening), the action has to be aware of "nothing-to-do-yet" as well
  • listen to the worker, when receiving the DONE replace/fill the data into the combo's model
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文