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 4006386
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:30:31+00:00 2026-05-20T08:30:31+00:00

In the below code the JProgressBar displays correctly when the doSomething() is called from

  • 0

In the below code the JProgressBar displays correctly when the doSomething() is called from within main() but not when called as a result of an ActionEvent – the interface seems to freeze. What is the problem?

import java.awt.BorderLayout; 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Vector;


public class ThreadedDialog  extends JFrame implements ActionListener{                
    private JDialog dlg;
    private JButton button;

    private void buildInterface(){
        button = new JButton("do stuff;");
        button.addActionListener(this);
        this.getContentPane().setLayout(new BorderLayout()); 
        this.getContentPane().add(BorderLayout.CENTER, button); 


        dlg = new JDialog(this, "Progress Dialog", true); 
        JProgressBar dpb = new JProgressBar(0, 500); 
        dlg.getContentPane().setLayout(new BorderLayout()); 
        dlg.getContentPane().add(BorderLayout.CENTER, dpb); 
        dlg.getContentPane().add(BorderLayout.NORTH, new JLabel("Progress..."));         
        dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); 
        dlg.setSize(300, 75); 
        dlg.setLocationRelativeTo(this);
        dpb.setIndeterminate(true);        
    }

    public void doSomething(){
        Thread t = new Thread(new Runnable(){ 
            public void run() { 
                dlg.show(); 
            } 
        });                          
        t.start();     
        try { 
            for (int i=0; i<100; i++){
                System.out.println("wtf is going on here?");
                Thread.sleep(5000);
            }
        } catch (InterruptedException e) { 
            e.printStackTrace(); 
        }
        dlg.hide();         
    }

    public static void main(String[] args) { 
        ThreadedDialog me = new ThreadedDialog();

        me.buildInterface();        
        me.pack();
        me.setVisible(true);
        me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        //me.doSomething();
    }

    public void actionPerformed(ActionEvent event) {
        doSomething();
    }
} 

Thanks

  • 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-05-20T08:30:31+00:00Added an answer on May 20, 2026 at 8:30 am

    Everything you do with Swing components should be done on the event dispatch thread (EDT) (i.e. the thread used by Swing to call your events). You should launch threads to perform lengthy background operations.

    In your code, you do the reverse : you try showing the dialog in another thread, and perform the long operation in the EDT.

    Here’s the fixed code :

    package fr.free.jnizet.stackoverflow;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JProgressBar;
    import javax.swing.SwingUtilities;
    
    
    public class ThreadedDialog  extends JFrame implements ActionListener{
        private JDialog dlg;
        private JButton button;
    
        private void buildInterface(){
            button = new JButton("do stuff;");
            button.addActionListener(this);
            this.getContentPane().setLayout(new BorderLayout());
            this.getContentPane().add(BorderLayout.CENTER, button);
    
    
            dlg = new JDialog(this, "Progress Dialog", true);
            JProgressBar dpb = new JProgressBar(0, 500);
            dlg.getContentPane().setLayout(new BorderLayout());
            dlg.getContentPane().add(BorderLayout.CENTER, dpb);
            dlg.getContentPane().add(BorderLayout.NORTH, new JLabel("Progress..."));
            dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
            dlg.setSize(300, 75);
            dlg.setLocationRelativeTo(this);
            dpb.setIndeterminate(true);
        }
    
        public void doSomething(){
            // create a thread for the background task
            Thread t = new Thread(new Runnable(){
                public void run() {
                    try {
                        for (int i=0; i<100; i++){
                            System.out.println("wtf is going on here?");
                            Thread.sleep(5000);
                        }
                    }
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // when the background task is finished, hide the dialog in the EDT.
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            dlg.setVisible(false);
                        }
    
                    });
                }
            });
            t.start();
    
            // show the dialog in the EDT
            dlg.setVisible(true);
        }
    
        public static void main(String[] args) {
            // create the GUI in the EDT
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    ThreadedDialog me = new ThreadedDialog();
    
                    me.buildInterface();
                    me.pack();
                    me.setVisible(true);
                    me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                }
            });
    
        }
    
        public void actionPerformed(ActionEvent event) {
            doSomething();
        }
    }
    

    You should read this tutorial, and learn to use SwingWorker for background tasks.

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

Sidebar

Related Questions

Below code not work, but it's work fine for jsf1.2. Now the framework is
The below code works in Firefox but not in Google Chrome: <!DOCTYPE html> <html>
Below code works fine in Firefox, but not in IE8. It triggers mouse click
Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
below code is my databasehandler class i got it from a tutorial. Beside that
In below code when I click 'Vote' a vote results screen is displayed but
I have a simple MFC program which displays the progressbar..I used the below code
I have little problem in my iPhone application code but can not identify. please
Below code is for donwnloading file from azure blob. I have problem with .docx,.xlsx
The below code makes application freeze when changing focus from Edit1 to Edit2 after

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.