ThreadUtil – how to update the user interface within a thread?

Home Forums Development ThreadUtil – how to update the user interface within a thread?

  • Frederic Marion-Poll

    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

    Found 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

    Glad 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

    oh! thanks. I did not guess it from reading the description of these functions.

    fred

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘Development’ is closed to new topics and replies.