Tagged: Java threading
-
Frederic Marion-Poll June 25, 2019 at 10:30 pm
Hi all,
I am reading a series of TIFF files which are really slow to read (and save) to disk. The names of these files is stored into a Swing JComboBox, and each image is displayed into a separate window . Since the whole process is painfully slow, I tried to insert the routine within a ThreadUtil, like this:
List <String> names; // array with a series of filenames to open
ThreadUtil.bgRun( new Runnable() { @Override public void run() {
openFilesFromDisk(names);transferFileNamesToComboBox(names);
openWindowsToDisplayEachFile(names);
}});
Updating the combobox and displaying the images within the thread issues 3 errors like:
- org.pushingpixels.substance.api.UiThreadingViolationException: Component creation must be done on Event Dispatch Thread
- org.pushingpixels.substance.api.UiThreadingViolationException: State tracking must be done on Event Dispatch Thread
- org.pushingpixels.substance.api.UiThreadingViolationException: Component state change must be done on Event Dispatch Thread
2 questions:
- is there a swing thread that we can use to update user interface elements from within a calculation thread?
- is there a way to wait until the ThreadUtil.bgRun is finished and then only update the user interface element?
- should I implement sub-threads?
I suspect it is a classical issue in Java, but I am stuck…
Thank you in advance for any suggestion. Fred
Frederic Marion-Poll June 25, 2019 at 10:39 pmFound a solution, which is to include the components update (jComboBox and graphics creation) into another thread, from SwingUtilities (javax.swing.SwingUtilities):
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});No more bug and warnings then…
Fred
Stephane Dallongeville June 26, 2019 at 4:00 pmGlad you found the solution 🙂
Indeed AWT component need to be updated from the AWT thread. Icy does provide methods to do it as well as these methods are quite useful when you deal with GUI:
ThreaUtil.invokeLater(..), ThreadUtil.invokeNow(..)Best,
– Stephane
Frederic Marion-Poll June 26, 2019 at 7:12 pmoh! thanks. I did not guess it from reading the description of these functions.
fred
The forum ‘Development’ is closed to new topics and replies.