Monitor progresses of tasks in JSwing

Monitoring the progress of tasks is a friendly design for users, especially for tasks that take a long time. In this tutorial, we will teach you to use the progress bar of Java Swing to monitor the progress of tasks.

You will learn how to:

  • Understanding event handler "stateChanged" timing of the use
  • Creating an event handler to show progress when the value is changed

 

Prerequisites

Before you begin to read this tutorial, you must:

  1. Familiar with Java swing development, go to oracle Java Swing to get the tutorial.
  2. Read the first three tutorials on JSwing.
  3. Familiar with the Operation of Apache NetBeans.
  4. Familiar with the timing and object of stateChanged be called.

 

Create a MonitorProgress Form

  1. Create a JFrame class named MonitorProgress on the form of the packages, and then drag the necessary components onto the form, the tree diagram as shown in the following figure.

    MonitorProgress tree-view

  2. Change the properties of the components as shown in the table below.
    ComponentstextactionCommandname
    jButton1StartStartStart
    jButton2ExitExitExit
    jLbael1Phase-1  
    jLbael2Phase-2  
    jLbael3Phase-3  
    jLbael4Phase-4  
    jLbael5Phase-5  
    jCheckBox1Check-1 Check-1
    jCheckBox2Check-2 Check-2
    jCheckBox3Check-3 Check-3
    jCheckBox4Check-4 Check-4
    jCheckBox5Check-5 Check-5
    jProgressBar1  P0
    jProgressBar2  P1
    jProgressBar3  P2
    jProgressBar4  P3
    jProgressBar5  P4
    jProgressBar6  P5
  3. After the successful step above, Press Ctrl-S to save the file, and press F9 to Compile the Form, shown as the following.

    MonitorProgress Form

Create a MonitorProgress App

Create a Java class named MonitorProgress on the app of the Packages, and then add code to the Class.

package app;

import java.util.logging.Level;
import java.util.logging.Logger;
import jswing.application.*;

public final class MonitorProgress extends App {

    public static void main(String args[]) {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Form.open("form.MonitorProgress","MonitorProgress");
                UserInterface.initialize("app.MonitorProgress",Form.getCurrentForm());
            }
        });
    }
    
    @Override
    public void stateChanged(java.awt.Component component) {
        if(component.equals(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P1"))) ||
           component.equals(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P2"))) ||     
           component.equals(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P3"))) ||     
           component.equals(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P4"))) ||
           component.equals(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P5")))) {
            //System.out.println("stateChanged");
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P1")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P1")).getGraphics());
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P2")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P2")).getGraphics());
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P3")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P3")).getGraphics());
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P4")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P4")).getGraphics());
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P5")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P5")).getGraphics());
            int i = ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P1")).getValue() * 20 / 100 +
                    ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P2")).getValue() * 20 / 100 +
                    ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P3")).getValue() * 20 / 100 +
                    ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P4")).getValue() * 20 / 100 +
                    ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P5")).getValue() * 20 / 100;
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P0")).setValue(i);
            ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P0")).update(((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P0")).getGraphics());
        }
    }
    
    @Override
    public void actionPerformed(java.awt.event.ActionEvent e){
        String action = e.getActionCommand();
        switch(action){
            case "Start":
                start();
                break;
            case "Exit":
                System.exit(0);
                break;
        }
    }
    private void start() {
        for(int i = 0; i <= 100; i ++){
                ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P1")).setValue(i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MonitorProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-1")).setSelected(true);
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-1")).update(((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-1")).getGraphics());
        
        for(int i = 0; i <= 100; i ++){
                ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P2")).setValue(i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MonitorProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-2")).setSelected(true);
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-2")).update(((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-2")).getGraphics());
        
        for(int i = 0; i <= 100; i ++){
                ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P3")).setValue(i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MonitorProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-3")).setSelected(true);
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-3")).update(((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-3")).getGraphics());    
        
        for(int i = 0; i <= 100; i ++){
                ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P4")).setValue(i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MonitorProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-4")).setSelected(true);
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-4")).update(((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-4")).getGraphics());
        for(int i = 0; i <= 100; i ++){
                ((javax.swing.JProgressBar)Form.getCurrentForm().getComponentByName("P5")).setValue(i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                Logger.getLogger(MonitorProgress.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-5")).setSelected(true);
        ((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-5")).update(((javax.swing.JCheckBox)Form.getCurrentForm().getComponentByName("Check-5")).getGraphics());
    }
}

 

Code Explained

  1. stateChanged.

    stateChanged gets called when the value is changed. In this tutorial, we assign a task consisting of five steps, all steps have their progress, and finally, the progress of tasks gets counted through sub-steps.

 

Run application

Press Ctrl-S to save the file, Shift-F6 to Run the application, and then you will see the progress of tasks when Press start.

MonitorProgress

 

Exercises

  1. Design an application that copies a file and displays the progress.

Next steps

Click download source code or next for more tutorials.

Share this page

Let more people know about this site, please share