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

  • SEARCH
  • Home
  • 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 8133183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:31:52+00:00 2026-06-06T09:31:52+00:00

I would like to make JProgressBar in new JDialog, witch will be in separate

  • 0

I would like to make JProgressBar in new JDialog, witch will be in separate thread from main logic. So I can start indeterminate progress with just creating new JDialog and completing that progress with disposing JDialog. But it gives me hard time to achieve that because after JDialog appears it doesn’t show any components (including JProgressBar) until the logic in main thread (SwingUtilities) is done.

Thread including JDialog:

package gui.progress;

public class ProgressThread extends Thread {
    private ProgressBar progressBar = null;

    public ProgressThread() {
        super();
    }

    @Override
    public void run() {
        progressBar = new ProgressBar(null);
        progressBar.setVisible(true);
    }

    public void stopThread() {
        progressBar.dispose();
    }
}

JProgressBar toggle method:

private static ProgressThread progressThread = null;
...
public static void toggleProcessBar() {
    if(progressThread == null) {
        progressThread = new ProgressThread();
        progressThread.start();
    } else {
        progressThread.stopThread();
        progressThread = null;
    }
}
  • 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-06T09:31:53+00:00Added an answer on June 6, 2026 at 9:31 am

    But it gives me hard time to achieve that because after JDialog appears it doesn’t show any components (including JProgressBar) until the logic in main thread (SwingUtilities) is done.

    you have issue with Concurrency in Swing, Swing is single threaded and all updates must be done on EventDispatchThread, there are two ways

    • easies to use Runnable#Thread, but output to the Swing GUI must be wrapped into invokeLater

    • use SwingWorker, example about SwingWorker is in the Oracles JProgressBar and SwingWorker tutorial

    EDIT

    this code simulate violating EDT and correct workaround for SwingWorker too

    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.Window;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyChangeEvent;
    import java.beans.PropertyChangeListener;
    
    import javax.swing.*;
    
    public class TestProgressBar {
    
        private static void createAndShowUI() {
            JFrame frame = new JFrame("TestProgressBar");
            frame.getContentPane().add(new TestPBGui().getMainPanel());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowUI();
                }
            });
        }
    
        private TestProgressBar() {
        }
    }
    
    class TestPBGui {
    
        private JPanel mainPanel = new JPanel();
    
        public TestPBGui() {
            JButton yourAttempt = new JButton("Your attempt to show Progress Bar");
            JButton myAttempt = new JButton("My attempt to show Progress Bar");
            yourAttempt.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    yourAttemptActionPerformed();
                }
            });
            myAttempt.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    myAttemptActionPerformed();
                }
            });
            mainPanel.add(yourAttempt);
            mainPanel.add(myAttempt);
        }
    
        private void yourAttemptActionPerformed() {
            Window thisWin = SwingUtilities.getWindowAncestor(mainPanel);
            JDialog progressDialog = new JDialog(thisWin, "Uploading...");
            JPanel contentPane = new JPanel();
            contentPane.setPreferredSize(new Dimension(300, 100));
            JProgressBar bar = new JProgressBar(0, 100);
            bar.setIndeterminate(true);
            contentPane.add(bar);
            progressDialog.setContentPane(contentPane);
            progressDialog.pack();
            progressDialog.setLocationRelativeTo(null);
            Task task = new Task("Your attempt");
            task.execute();
            progressDialog.setVisible(true);
            while (!task.isDone()) {
            }
            progressDialog.dispose();
        }
    
        private void myAttemptActionPerformed() {
            Window thisWin = SwingUtilities.getWindowAncestor(mainPanel);
            final JDialog progressDialog = new JDialog(thisWin, "Uploading...");
            JPanel contentPane = new JPanel();
            contentPane.setPreferredSize(new Dimension(300, 100));
            final JProgressBar bar = new JProgressBar(0, 100);
            bar.setIndeterminate(true);
            contentPane.add(bar);
            progressDialog.setContentPane(contentPane);
            progressDialog.pack();
            progressDialog.setLocationRelativeTo(null);
            final Task task = new Task("My attempt");
            task.addPropertyChangeListener(new PropertyChangeListener() {
    
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName().equalsIgnoreCase("progress")) {
                        int progress = task.getProgress();
                        if (progress == 0) {
                            bar.setIndeterminate(true);
                        } else {
                            bar.setIndeterminate(false);
                            bar.setValue(progress);
                            progressDialog.dispose();
                        }
                    }
                }
            });
            task.execute();
            progressDialog.setVisible(true);
        }
    
        public JPanel getMainPanel() {
            return mainPanel;
        }
    }
    
    class Task extends SwingWorker<Void, Void> {
    
        private static final long SLEEP_TIME = 4000;
        private String text;
    
        public Task(String text) {
            this.text = text;
        }
    
        @Override
        public Void doInBackground() {
            setProgress(0);
            try {
                Thread.sleep(SLEEP_TIME);// imitate a long-running task
            } catch (InterruptedException e) {
            }
            setProgress(100);
            return null;
        }
    
        @Override
        public void done() {
            System.out.println(text + " is done");
            Toolkit.getDefaultToolkit().beep();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a php script that can capture a page from
Would like to make anapplication in Java that will not automatically parse parameters used
I'm extremely new with jQuery and I would like make sure I'm writing it
I would like to make a package that would copy data from a table
I would like to make sure that when user log in it will stay
I would like to make phone calls from my app. I m using that
I would like to make two pairs from pairs. A pair consists of two
I would like make all text within div.main gray except for all content within
I would like make an extension method for the generic class A which takes
I would like to make a LaTeX document with two tables of contents, like:

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.