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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:30:40+00:00 2026-06-14T06:30:40+00:00

I have a server call from the UI. It has response time is little

  • 0

I have a server call from the UI. It has response time is little high. So I was thinking to display a progress bar during data loading from the server. I have tried the following code using this approach to show the progress bar. Some where I am doing wrong I am not seeing the progress bar when I call the calculateResult() method on button click. I no need to display any percentage on the progress bar. It just needs to show that data is loading.

// The following code I have tried.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

 public class MyProgressBarTest extends JFrame {
private static final long serialVersionUID = 1L;

private static JProgressBar progressBar;

public static void main(String[] args) {
    MyProgressBarTest obj = new MyProgressBarTest();
    obj.createGUI();
}

public void createGUI() {
    JPanel panel = new JPanel();
    JButton button = new JButton("Progress");

    progressBar = new JProgressBar();

    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {

            MyCustomProgressBarDialog progressBarObj = new    MyCustomProgressBarDialog(progressBar);
            progressBarObj.createProgressUI();

            MyActionPerformer actionObj = new MyActionPerformer(progressBar);
            actionObj.execute();

            progressBarObj.setVisible(false);
        }
    });

//      panel.add(progressBar);
    panel.add(button);
    add(panel);

    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setLocationRelativeTo(null);
//      pack();
    setSize(200, 300);
    setVisible(true);
}
}

class MyActionPerformer extends SwingWorker<String, Object> {

JProgressBar fProgressBar;

public MyActionPerformer(JProgressBar progressBar) {
    this.fProgressBar = progressBar;
    this.fProgressBar.setVisible(true);
    this.fProgressBar.setIndeterminate(true);
}

protected String doInBackground() throws Exception {

    calculateResult();
    return "Finished";
}

protected void done() {
    fProgressBar.setVisible(false);
}

public void calculateResult() {
    for (int i = 0; i < 500000; i++) {
        System.out.println("Progress Bar: " + i);
    }
}
}

class MyCustomProgressBarDialog extends JDialog {
private static JProgressBar progressBar;

public MyCustomProgressBarDialog(JProgressBar progressBar) {
    this.progressBar = progressBar;
}

public void createProgressUI() {
    add(progressBar);
    setLocationRelativeTo(null);
    setSize(50, 20);
    setVisible(true);
}
}
  • 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-14T06:30:42+00:00Added an answer on June 14, 2026 at 6:30 am

    The reason your progress bar disappears immediately is your ActionListener

    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
       MyCustomProgressBarDialog progressBarObj = new    MyCustomProgressBarDialog(progressBar);
       progressBarObj.createProgressUI();
    
       MyActionPerformer actionObj = new MyActionPerformer(progressBar);
       actionObj.execute();
    
       progressBarObj.setVisible(false);
      }
    });
    

    The actionObj.execute(); method is not blocking (good thing or it would be useless) meaning that immediately after you start the SwingWorker with that call you will execute the progressBarObj.setVisible(false); statement.

    This causes the progress bar dialog to disappear.

    I can think of 2 solutions for this

    • Pass the dialog to the SwingWorker as well and call setVisible( false ) on the dialog in the done method of the SwingWorker
    • A SwingWorker fires PropertyChangeEvents which allow you to determine how far it progressed. You can use such a listener to hide the dialog when the calculations are finished
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get array of images that have to call from server and
I have generic async task class which fetches response from server . And i
I have a domain service, derived from LinqToEntitiesDomainService<FOOEntities> It has one method, IQueryable<Bar> GetBar().
I have an Activity that makes a remote server call and tries to populate
I have a server application with such structure: There is one object, call him
Let's say I have two SQL Server databases running on separate machines, call them
I have a GWT app that makes an RPC call to a server to
I have come up with the following code to call a webservice using client/server
I have a UDF in sql server. I want to call this function at
I'm using SQL Server 2005 I have a user. We'll call him User1. He

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.