I have an ordinary JTable and a ListSelectionListener. It works as it should, I guess, but there is one problem:
- I click on a table row
- The event fires (method valueChanged kicks in)
- Inside valueChanged I start an http request (takes a couple of ms)
- The table row gets visibly selected (blue background)
This produces a noticable delay.
Can I change the order of the events? I'd like to have the blue background first, then doing the http request. Inserting a sleep timer in the valueChanged method makes the selection wait until the timer is finished.
You should probably do the HTTP request on a background thread. This will also have the advantage of keeping the UI responsive if it takes an abnormally long time.
You'd end up with code something like this, depending on what your needs actually are:
doInBackgroundcan generally take any action as long as you don't interact with the Swing UI from it.Also see the
SwingWorkertutorials and documentation.For the sake of a complete answer, doing the following at the start of the list selection event may work:
That's at least insofar as
paintImmediatelyis specified to do what's implied:The reason I wouldn't recommend using that is that there's no particular reason that the
JListneeds to be updated before our own listener such that the list would actually paint the new selection.There's also the issue that heavy-duty tasks like HTTP requests generally shouldn't be done on the Swing thread, because it freezes the UI. Even in cases where you want the user to wait for awhile, you wouldn't do the task directly on the Swing thread. You'd do it in a background thread and pop up a modal dialog box, possibly giving the user the option to cancel the task prematurely.