java progress bar

Modal Progress Bar Dialog with Java Swing

In this post I will show you how you can make a modal progress bar dialog in a Java Swing application. Though I am a Java programmer I am mainly involved in web development so my experience with Swing is rather limited.

The problem

So you have an application made in Java with Swing and in it you have for example a button that the user can push to start processing something. Main challenge for me coming from web development was that with Swing you have to be aware of the UI thread.

That means that if you start processing something and that takes a lot of time that will also block the UI thread and the application will look like it has frozen. Not too nice for user experience. What you can do is do all processing on a separate thread and to further improve user experience you can add a progress bar window indicating that the application is working on something. What would be even nicer in some cases (was also my case) is if you could somehow prevent the user from modifying anything in the application as that might influence the result of the processing in unwanted ways.

The solution

The solution is to create a modal progress bar dialog and display it while the application is processing, prevent the user from closing this dialog and close it once the processing finishes. Easier said than done (if you don’t know how things work in Swing). If you open up a modal dialog in the application, everything stops until the  dialog is closed somehow.

Consider the following code:

...
JDialog dlgProgress = new JDialog(null, "Please wait...", true);//true means that the dialog created is modal
...
/*code creating the contents of the progress dialog*/
...
dlgProgress.setVisible(true);
longProcessingTask(); // this will only be executed after the user closes the dialog
...

As the comment says, the longProcessingTask() method will only be executed after the dialog is closed because dlgProgress is a modal dialog and  dlgProgress.setVisible(true) will block the execution flow. What we need is a way to start processing on a separate thread, and while that is happening display the progress bar dialog. Also, when the processing is done we should close the progress bar dialog.

To start processing on a separate thread Java has all sort of options, but as we are in Swing let’s use something that Swing provides: SwingWorker.

Here’s the resulting code:

...
JDialog dlgProgress = new JDialog(null, "Please wait...", true);//true means that the dialog created is modal
JLabel lblStatus = new JLabel("Working..."); // this is just a label in which you can indicate the state of the processing

JProgressBar pbProgress = new JProgressBar(0, 100);
pbProgress.setIndeterminate(true); //we'll use an indeterminate progress bar

dlgProgress.add(BorderLayout.NORTH, lblStatus);
dlgProgress.add(BorderLayout.CENTER, dpb);
dlgProgress.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); // prevent the user from closing the dialog
dlgProgress.setSize(300, 90);

SwingWorker<Void, Void> sw = new SwingWorker<Void, Void>() {
 @Override
 protected Void doInBackground() throws Exception {
 longProcessingTask();
 return null;
 }

 @Override
 protected void done() {
 dlgProgress.dispose();//close the modal dialog
 }
};

sw.execute(); // this will start the processing on a separate thread
dlgProgress.setVisible(true); //this will block user input as long as the processing task is working
...

 

Hope you can put this to good use!

John Negoita

View posts by John Negoita
I'm a Java programmer, been into programming since 1999 and having tons of fun with it.

6 Comments

  1. David GallardoMay 12, 2015

    Thank you…. excelent.

    Reply
  2. MatteoAugust 27, 2015

    The code above is not correct. Reply
    dlgProgress.add(BorderLayout.CENTER, dpb);

    with

    dlgProgress.add(BorderLayout.CENTER, pbProgress);

    Reply
  3. FlavioJuly 15, 2016

    Thanks!!!!

    Reply
    1. John NegoitaJuly 16, 2016

      Hi Flavio,

      glad to be of help!

      Reply
  4. BeppinoFebruary 28, 2018

    All I needed!
    Thanks a lot, great!

    Reply
    1. John NegoitaMarch 2, 2018

      Happy to help!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top