Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:11:09+00:00 2026-06-17T09:11:09+00:00

I have problem with handling threads in my application. It creates JFrame and starts

  • 0

I have problem with handling threads in my application. It creates JFrame and starts a new Thread. Last one will execute external application and update GUI. Then

I have problem to make Main class to wait for second thread to finish, but also to update GUI simultaneously.

Here’s my example (shortened):

class Main {

    public int status;

    public Main() {

        // Creating GUI etc.

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                JDialog id = new JDialog();
                id.button.addMouseListener(new MouseListener()); // Calls generate() method
            }

        });

    }

    public void generate() {

        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                // Make changes to GUI
            }

        });

        GeneratorThread genTest = new GeneratorThread(this, 1, 1, 1);
        genTest.start();

        //while (status == 0);

        System.out.println("Next step.");

    }

}

And Thread class:

public class GeneratorThread extends Thread {

protected Main main;
protected int setSize, minValue, maxValue;

public GeneratorThread(Main main, int setSize, int minValue, int maxValue) {
    this.main = main;
    this.setSize = setSize;
    this.minValue = minValue;
    this.maxValue = maxValue;
}

public void run() {

    // Execute program etc.
    // Change GUI from main in the same time
            // About 3 seconds

    main.status = 1;

}

}

I’m in progress and I wanted to check how it works so far. While worked nice, but it locks Swing somehow and any changes are visible only when GeneratorThread finishes. I would like to update GUI in the real time.

I’ve tried join(), effects are the same. I also tried wait() (on Main), but then I got IllegalStateMonitorException.

Any hints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:11:10+00:00Added an answer on June 17, 2026 at 9:11 am

    Swing is a single threaded environment. That is, there is a single thread responsible for managing all the interactions and updates to the Swing UI – the Event Dispatching Thread.

    Among the golden rules of Swing are…

    • DON’T block the EDT (Thread.sleep, Thread#join, Object#wait, block IO and/or time consuming tasks (among others) should never be called from within the EDT), doing so will stop the EDT from dispatching events and paint updates (amongst other things)
    • ONLY create/update Swing UI elements from within the EDT.

    This raises a question…how do you “wait” for a thread?

    The best way is use an Observer pattern. Basically, you provide the Thread with some kind of reference that it will call to provide notification of events, such as errors and completion…

    This will require you to think very carefully about the design of your applications, as you can not rely on a simple A to B execution of your code.

    For example…

    public class TestThreadCallBack {
    
        public static void main(String[] args) {
            new TestThreadCallBack();
        }
    
        public TestThreadCallBack() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public interface ThreadCallBack {
    
            public void threadCompleted(Runnable source);
    
            public void threadFailed(Runnable source);
        }
    
        public class TestPane extends JPanel implements ThreadCallBack {
    
            private JLabel message;
            private JLabel dots;
            private int count;
    
            private Timer timer;
    
            public TestPane() {
                setLayout(new GridBagLayout());
                message = new JLabel("Running background task, please wait");
                dots = new JLabel("   ");
                add(message);
                add(dots);
    
                timer = new Timer(250, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        count++;
                        if (count > 3) {
                            count = 0;
                        }
                        StringBuilder sb = new StringBuilder(3);
                        for (int index = 0; index < count; index++) {
                            sb.append(".");
                        }
                        for (int index = count; index < 3; index++) {
                            sb.append(" ");
                        }
                        dots.setText(sb.toString());
                    }
                });
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();
    
                Thread thread = new Thread(new BackgroundTask(this));
                thread.start();
    
            }
    
            @Override
            public void threadCompleted(Runnable source) {
                timer.stop();
                message.setText("Task completed successfully");
            }
    
            @Override
            public void threadFailed(Runnable source) {
                timer.stop();
                message.setText("Task failed");
            }
        }
    
        public class BackgroundTask implements Runnable {
    
            private ThreadCallBack callBack;
    
            public BackgroundTask(ThreadCallBack callBack) {
                this.callBack = callBack;
            }
    
            @Override
            public void run() {
                System.out.println("Background task underway...");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException interruptedException) {
                }
                int result = (int) Math.round((Math.random() * 1));
                if (result == 0) {
                    callBack.threadCompleted(this);
                } else {
                    callBack.threadFailed(this);
                }
            }
        }
    }
    

    Updating the UI from within a Thread other then the EDT is, well, messy. An easier solution would actually be to use a SwingWorker. This has publish/process methods that make easy to update the UI and progress methods that can be used to provide feedback about the progress of the current task.

    You can use it’s done method to notify interested parties when the worker has completed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have one problem with handling the thread in android ,in my class i
i have one problem with handling list,i have three class named as UserInf,userData,userProcess,i created
I am handling a thread in my application, I am facing a problem with
I have a thread which polls a folder for new files. The problem is
i have an problem with handling data come from server , please see the
I have a problem with slashes! I have some jQuery for handling generic dialogs
I experience strange problem. We have error handling in global.asax that would redirect user
I have problem with show or hide form in Window Form Application. I start
I have a semi-complicated problem and hoping that someone here will be able to
I have a two threads, one which works in a tight loop, and the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.