How to indicate that JComboBox is loading values?

I have a JComboBox whose values ​​are retrieved over the network.

I am looking for a way to indicate this fact to the user when the user wants to see the list, extends the drop-down list, and only then the data is retrieved.

Primary requirements:

  • The JComboBox popup should not block EDT, but the combo action should not work until there are values.
  • The user must know when all the data has been received.
  • The size (UI real-estate) of the indicator should be as small as possible.

Note that data is not retrieved until the user wants to see the combined values ​​(i.e., expanding the drop-down list).

The solution I used:

I used SwingWorker to support the user interface. The combo box was superimposed using JIDE Overlayable with a JIDE InfiniteProgressPanel that listens to the worker.

+6
source share
3 answers

To avoid blocking EDT, your data lookup should be done in the background thread. I would use SwingWorker to find and load values, as this makes it possible to use the background stream with other goodies that make it very convenient for Swing. I would enable the JComboBox enabled false property until all values ​​are loaded, and then enable it through setEnabled(true) . You will find out that SwingWorker is executed either using the done() method (by overriding), or by adding a PropertyChangeListener to SwingWorker and notifying it when its status is SwingWorker.StateValue.DONE .

One way to find out that the process is complete is that they will see when the combo box has been re-enabled. If you need a more obvious indicator, you can display a JProgressBar or ProgressMonitor. This may appear in the dialog box if you want to leave the appearance of the graphical interface practically unchanged.

+7
source

I implemented it by adding the "Loading ..." element and a special border around the JComboBox. A separate stream begins on the click, adding new elements through SwingUtilities.invokeAndWait. When the download is complete, the last "Download ..." item is deleted.

+3
source

so as not to make my users wait until the data is downloaded, combine the answers with eel and stan :-)

  • start with a model containing zero or one real value plus the dummy entry "loading"
  • register PopupMenuListener and launch SwingWorker, loading the data (into a separate data structure, maybe a new model) in the very first menu of WillBecomeVisible
  • during boot, select a dummy entry (and / or whatever else is needed to tell the user what is going on), the action should also be aware of "nothing done"
  • Listen to the employee; when receiving DONE, replace / fill in the data in the combined model
+1
source

Source: https://habr.com/ru/post/904493/


All Articles