1

I try to create and show a GUI and to populate later a DefaultComboBoxModel using SwingUtilities.invokeLater(...).

But the UI is still freezing and components are not displayed until the loading thread has completed.

The main frame is instantiated by Spring application context.Building/showing GUI through "fullInitialize" method called by Spring after frame initialization has completed (see method code below).

    <bean id="view" class="be.philippefery.bigmap.view.MainView" init-method="fullInitialize">
        <constructor-arg index="0" ref="applicationTitle" />
        <property name="i18n" ref="i18n" />
        <property name="progressListener" ref="progressBar" />
        <property name="jb_importGpx" ref="gpxImport" />
        <property name="jb_importPointsOfInterest" ref="poisImport" />
        <property name="jp_gpxList" ref="gpxFilteredTablePanel" />
        <property name="jp_poiList" ref="poiFilteredTablePanel" />
    </bean>

MainView class (MainView extends JFrame):

    private void fullInitialize() throws IOException {
        //Creating the UI here
        //This includes a JList and its model which should be populated later (see below)
        createAndShowGUI();             

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    //Time consuming task sending HTTP requests to check if a list of servers are available
                    //then filling a listModel instantiated through createAndShowGUI() method
                    initializeTilesServerList();                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    private void createAndShowGUI() throws IOException {
        this.initialize();
        this.jsonUtils = new JsonUtils();
        this.createComponents();
        this.buildLayout();
        pack();
        setVisible(true);
    }

The problem I'm having is that, even when populating the list using SwingUtilities.invokeLater(...), the UI isn't displayed until this thread is terminated.

This is what is displayed while SwingUtilities.invokeLater(...) is running: while thread is running

... and once terminated: once thread terminated

Any idea?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    I think you misunderstand the purpose of method `invokeLater`. Refer to [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) lesson in Oracle's Java tutorials. – Abra Feb 18 '22 at 10:12
  • Thank your for you comment Abra but I don't see where the mistake is. Do you mean I should use a SwingWorker instead? – Philippe Fery Feb 18 '22 at 10:36
  • *"I should use a SwingWorker instead"* That's what I'd have thought @Abra meant, yes. – Andrew Thompson Feb 18 '22 at 10:38
  • Thank you Andrew. I give it a try... – Philippe Fery Feb 18 '22 at 10:53
  • 1
    This was the solution. It's working fine now using a SwingWorker. Thank you for your help Abra and @Andrew Thompson ;-) – Philippe Fery Feb 18 '22 at 11:10
  • *"Thank you for your help Abra"* And plus one for implementing the suggestion of @Abra without 99 back and forth comments and instead picking up the ball and running with it. I've seen too much of the opposite of that recently, and this was a refreshing change. Glad you got it sorted. :) – Andrew Thompson Feb 18 '22 at 16:49

0 Answers0