Sometimes we need to know about the progress of an activity that is going on in our system. The progress bar is the graphical user interface component that doest that job of showing the progress of the activity. sometimes it is used in the Games to show how much life is remaining. 

Progress helps us to track the activity on how much percentage it is done. It also alerts us once the activity is complete. we can have two types of progress bar either Horizontal progress bar or vertical progress bar. in Java, ProgressBar can be created by using the Java swing class name JProgressBar. 

constructors of the JProgressBar class:- 

As other Java swing component, the class JProgressBar has also overloaded constructors through which we can create our Progress bar instance. Some of them are given below with the explanation. 

JProgressBar():-

This is the default constructor of the JProgress Bar class. by using this constructor we will have a horizontal progress bar. The initial value that our progress will start from will be 0 and the final that our progress bar will end is 100. 

// creating the Default progress bar
JProgressBar progressBar = new JProgressBar();

JProgressBar(int o):-

By default the orientation of the Progress bar created with the default constructor of the Java swing package class JProgressbar is Horizontal. we can specify whether it is horizontal or vertical by passing the string constants to the JProgressbar constructor.

// creating the Progress bar with vertical orientation
JProgressBar progressBar1 = new JProgressBar(JProgressBar.VERTICAL);

JProgressBar(min,Max):- 

We can specify the initial and final value of the progress bar by using this constructor. the min and max value should be of type integer.

// creating the Progress bar with the min and max value
JProgressBar progressBar2 = new JProgressBar(0,1000);

JProgressBar(orient,min,max):-

By using this constructor we can set the orientation as well as the min and max value of the progress bar. 


// creating the Progressbar with the orintaion, min and max vaule
JProgressBar progressBar3 = new JProgressBar(JProgressBar.VERTICAL,0,50);

Creating a Simple Progress Bar:-

in this simple program, we will create a simple progress bar. we will look at how we can create our progress bar. we will enhance our program to the event handling. We will fill the progress bar with a loop. 

package AlixaProDev;

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

public class ProgressBarDemo {
ProgressBarDemo(){
// creating the Default progress bar
JProgressBar progressBar = new JProgressBar();
JFrame frame = new JFrame("AlixaProDev");
frame.setLayout(new FlowLayout());
frame.add(progressBar);

frame.setSize(new Dimension(500,400));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

}

public static void main (String[] args) {
new ProgressBarDemo();
}
}

This is how we can create our progress bar but to be honest, the above program is not doing anything. it is just a bar. There are some useful methods with the help of which we can create a very beautiful progress bar. Below is the description of some of these methods.

progressbar.setValue(int x):- 

With the help of this method, we set the value of the progress bar. it is helpful when we are working with a process. is the processes are going towards the completion we will update the progress bar value with the help of this method. 

// update the value of the progres bar to 30
progressBar.setValue(30);

progressbar.setString("string"):- 

with the help of this method, we can set the string that appears in the middle of the progress. for example, we can set the string perctage. 

// this will set the value of the bar to the string
progressBar.setString("AlixaProDev");

progressbar.getPercentComplete():-

when we want to grab the current percentage of completion we can do it by using this method. 

// get the perctage of completion
double complet = progressBar.getPercentComplete();

progressbar.setStringPainted(boolean b):-

the string will not print until and unless this method is not called with the true value. so to print the string make sure you set this method to true. 

// setting the progress bar string
progressBar.setStringPainted(true);

Creating our test program with all these methods that we have learned so far. 

Test Application:-

package AlixaProDev;

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

public class ProgressBarDemo {
JFrame frame = new JFrame();
JProgressBar progressBar = new JProgressBar();
ProgressBarDemo () {

// setting up our progress bar
progressBar.setValue(0);
progressBar.setBounds(0, 0, 420, 50);
progressBar.setStringPainted(true);
progressBar.setFont(new Font("MV Boli", Font.PLAIN, 20));
progressBar.setForeground(Color.green);
progressBar.setBackground(Color.BLACK);


frame.add(progressBar);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(420, 420);
frame.setLayout(null);
frame.setVisible(true);
try {
fill();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// method to fill our progress bar
public void fill() throws InterruptedException {
int counter =100;
while (counter>=0){
progressBar.setValue(counter);
Thread.sleep(50);
counter-=1;

}
progressBar.setString("Done");
}
public static void main (String[] args) {
new ProgressBarDemo();
}
}

We are updating the value of the progress bar after 50 milliseconds sleep in the while loop. it will be a very fast progress bar. we are updating the value in the loop but we can actually set it into some kind of tasks. we will see them in the upcoming articles. 
The output of the above code can be seen in the following short video. I will add a photo of that as well.

The Picture of the output after completion of the progress. 


The Video of the progress bar if you are interested you can watch this. 



Thank you, guys. see you in the next article. If you have any questions you can post it in the comment section. I will answer each of them.

You may also want to read.

Post a Comment

Previous Post Next Post