Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indicate that a JComboBox is loading values?

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.

like image 200
Asaf Avatar asked Dec 26 '11 15:12

Asaf


People also ask

Which of these is used to determine whether the JComboBox is editable?

SetEditable() - Determines whether the JComboBox field is editable.

What events does JComboBox generate?

JComboBox() : creates a new empty JComboBox . JComboBox(ComboBoxModel M) : creates a new JComboBox with items from specified ComboBoxModel. JComboBox(E [ ] i) : creates a new JComboBox with items from specified array. JComboBox(Vector items) : creates a new JComboBox with items from the specified vector.

Which method returns currently selected item in choice or JComboBox?

getRenderer. Returns the renderer used to display the selected item in the JComboBox field.


2 Answers

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.

like image 183
Hovercraft Full Of Eels Avatar answered Oct 17 '22 00:10

Hovercraft Full Of Eels


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.

like image 20
StanislavL Avatar answered Oct 16 '22 23:10

StanislavL