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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:51:52+00:00 2026-06-15T21:51:52+00:00

I am trying to add a progress bar. everything works and i don’t get

  • 0

I am trying to add a progress bar. everything works and i don’t get any error. But the progress bar goes from 0% to 100% without even going through the values between it (I mean it’s too fast, and the users are unable to see the progress bar blocks filling in)

pr = new JProgressBar();

            pr(0);
            pr(true);
..

public void iterate(){

        while (i<=20000){
            pr.setValue(i);
            i=i+1000;
            try{
                Thread.sleep(150);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

When i button is clicked i call the iterate() method, and i expect it to update the progress bar progressively. instead it pauses for a while and then displays a full progress bar.

How can i solve this ?

2.) I don’t like the default blue color of the progress bar tabs. I need to change the color. I tried pr.setForeground(Color.GRAY);
pr.setBackground(Color.RED);
But it didn’t work.

  • 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-15T21:51:53+00:00Added an answer on June 15, 2026 at 9:51 pm

    The problem is, you’re trying to update the progress within the context of the Event Dispatching Thread.

    This basically means that while you are in you loop, the EDT is unable to process any paint request you are making.

    What you need to do is some how offload the work to a separate thread and update the progress bar as needed. The problem with this, is you should never update the UI from any thread other then the EDT.

    But don’t despair, you have a number of options, the best is using a Swing Worker

    Updated with example

    enter image description here

    public class SwingWorkerProgress {
    
        public static void main(String[] args) {
            new SwingWorkerProgress();
        }
    
        public SwingWorkerProgress() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException 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 class TestPane extends JPanel {
    
            private JProgressBar pbProgress;
            private JButton start;
    
            public TestPane() {
    
                setBorder(new EmptyBorder(10, 10, 10, 10));
                pbProgress = new JProgressBar();
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.insets = new Insets(4, 4, 4, 4);
                gbc.gridx = 0;
                gbc.gridy = 0;
                add(pbProgress, gbc);
    
                start = new JButton("Start");
                gbc.gridy++;
                add(start, gbc);
    
                start.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        start.setEnabled(false);
                        ProgressWorker pw = new ProgressWorker();
                        pw.addPropertyChangeListener(new PropertyChangeListener() {
    
                            @Override
                            public void propertyChange(PropertyChangeEvent evt) {
                                String name = evt.getPropertyName();
                                if (name.equals("progress")) {
                                    int progress = (int) evt.getNewValue();
                                    pbProgress.setValue(progress);
                                    repaint();
                                } else if (name.equals("state")) {
                                    SwingWorker.StateValue state = (SwingWorker.StateValue) evt.getNewValue();
                                    switch (state) {
                                        case DONE:
                                            start.setEnabled(true);
                                            break;
                                    }
                                }
                            }
    
                        });
                        pw.execute();
                    }
                });
    
            }
        }
    
        public class ProgressWorker extends SwingWorker<Object, Object> {
    
            @Override
            protected Object doInBackground() throws Exception {
                int i = 0;
                int max = 2000;
    
                while (i < max) {
                    i += 10;
                    int progress = Math.round(((float)i / (float)max) * 100f);
                    setProgress(progress);
                    try {
                        Thread.sleep(25);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
    
                return null;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add a progress bar to my application, but I'm trying to
I am trying to add some text to a Tool Strip Progress Bar but
I was trying to add a progress bar to my Tkinter gui, and I
I am a PHP newbie and trying to add a progress-bar to an existing
I am trying to add a jquery progress bar inside a ul li tag.
I am trying to add a progress/loading bar to my application that uses WebView
I am getting this error.. I was trying to do progress bar that process
I´m currently trying to add parallel downloads to my application but I don´t know
Im new to asp.net mvc. I'm trying add new model class but it got
Trying to add a blank sample app for a rails tutorial to GitHub, but

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.