Progress Bar in Java

May 28, 2020
Tutorials
progress monitor

This tutorial will teach you how to make a progress monitor in java. A ProgressMonitor class just monitor progress in any operation. Therefore, we will not use the JProgressBar component here.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of progressMonitor.java.

2. Import the following package library:

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

3. Indicate the static global variables. To access the progress monitor in java, we will use the ProgressMonitor class. Have also the Timer and an Integer variable named progress.

static ProgressMonitor monitor;
static int progress;
static Timer timer;

4. Initialize your variables of the components in your Main.

JFrame frame = new JFrame("Progress Monitor");
JButton progressButton = new JButton("Click This!");

 

5. Now, we will perform an ActionListener named progressButtonActionListener to do the action when clicking the progressButton variable.

Here we will use the Component class to get the component which will perform the action and will use the variable monitor of the ProgressMonitor. We will register 0 as the starting number and 100 as our maximum number. And have your variable progress initializes to 0.

ActionListener progressButtonActionListener = new ActionListener() {
  public void actionPerformed(ActionEvent actionEvent) {
   Component parent = (Component) actionEvent.getSource();
   monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 100);
  progress = 0;

 

We can have our Progress Monitor on the off chance that it isn’t null. At that point insert inside if the timer is null, on the other hand, that the monitor variable will return its value. What’s more, if tapping the button Cancel it will display Monitor cancelled. Something else, the progress will proceed with its load that will increase its progress by 2 (by putting inside the progress variable in the monitor variable of Progress Monitor). At that point, it will have its note of Loading its percentage by increasing it to 2 and will stop at 100 as we declared that above.

if (monitor != null) {
  if (timer == null) {
    timer = new Timer(250, new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (monitor == null)
          return;
        if (monitor.isCanceled()) {
          System.out.println("Monitor canceled");
          timer.stop();
        } else {
          progress += 2;
          monitor.setProgress(progress);
          monitor.setNote("Loading:  " + progress + "%");
        }
      }
    });
  }

 

Now, start the timer.

timer.start();

Put the ActionListener variable to our created button.

progressButton.addActionListener(progressButtonActionListener);

 

6. Add the button using the add method of the frame. Set its layout to GridLayout using the setLayout method. Lastly, set the size, visibility, and have its close operation of the frame.

frame.getContentPane().add(progressButton);
 frame.getContentPane().setLayout(new GridLayout(0, 1));
frame.setSize(300, 200);
frame.setVisible(true);
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

Output:

output


Here’s the full code of this tutorial:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
 
public class progressMonitor{
 
  static ProgressMonitor monitor;
  static int progress;
  static Timer timer;
 
  public static void main(String args[]) {
    JFrame frame = new JFrame("Progress Monitor");
    JButton progressButton = new JButton("Click This!");
 
    ActionListener progressButtonActionListener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
       Component parent = (Component) actionEvent.getSource();
       monitor = new ProgressMonitor(parent, "Loading Progress", "Getting Started...", 0, 100);
      progress = 0;
        if (monitor != null) {
          if (timer == null) {
            timer = new Timer(250, new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                if (monitor == null)
                  return;
                if (monitor.isCanceled()) {
                  System.out.println("Monitor canceled");
                  timer.stop();
                } else {
                  progress += 2;
                  monitor.setProgress(progress);
                  monitor.setNote("Loading:  " + progress + "%");
                }
              }
            });
          }
          timer.start();
        }
      }
    };
 
 
    progressButton.addActionListener(progressButtonActionListener);
 
    frame.getContentPane().add(progressButton);
     frame.getContentPane().setLayout(new GridLayout(0, 1));
    frame.setSize(300, 200);
    frame.setVisible(true);
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.