Updated March 17, 2023
Introduction to JProgressBar
JProgressBar is considered to be a part of the Java Swing which is a package. It displays visually progress of some of the mentioned task. In addition, it demonstrates the percentage of the mentioned task completion. The bar in JProgressBar gets filled in as and when the mentioned task gets to its completion stage. JProgressBar also displays some text in addition to displaying the percentage of completion.
Swing API
Progress Monitoring API of the Swing includes a total of three classes that facilitate the use of the progress bars. The subclass of JProgressBar, JComponent is actually considered to be the graphical component that demonstrates the progress of the operation. Additionally, it also can get embedded within the other graphical components.
Constructors of JProgressBar
The constructors of JProgressBar are as under :
- JProgressBar(): This constructor is used to create the progress bar without any text on it.
- JProgressBar(int orientation): This constructor is used to create the progress bar along with the mentioned orientation in its parameter. In case VERTICAL is mentioned as a parameter then the vertical progress bar gets created and in case SwingConstants.Horizontal is mentioned as a parameter then the horizontal progress bar gets created.
- JProgressBar(int min, int max): This constructor is used to create the progress bar along with the mentioned minimum as well as the maximum value.
- JProgressBar(int orientation, int min, int max): This constructor is used to create the progress bar along with the mentioned minimum as well as the maximum value and also the specified orientation in the parameter. If SwingConstants.VERTICAL is mentioned as a parameter then the vertical progress bar gets created and in case SwingConstants. HORIZONTAL is mentioned as a parameter then the horizontal progress bar gets created.
Methods of JProgressBar
The methods of JProgressBar are as under :
- int getMaximum(): This method is used to return the maximum value of the progress bar.
- int getMinimum(): This method is used to return the minimum value of the progress bar.
- String getString(): This method is used to return the string representation of the current value of the progress bar.
- void setMaximum(int t): This method is used to set the maximum value of the progress bar to the value of t.
- void setMinimum(int t): This method is used to set the minimum value of the progress bar to the value of t.
- void setValue(int t): This method is used to set the current value of the progress bar to the value of t.
- void setString(String t): This method is used to set the value of the progress String to that of t which is a String.
Below is mentioned the syntax for the javax.swing.JProgressBar class.
Syntax:
public class JProgressBar extends JComponent implements SwingConstants, Accessible
Examples of JProgress
Here are some examples of JProgressBar which are given below with implementation:
Example #1 -Horizontal Progress Bar
Code:
import java.awt.*;
import javax.swing.*;
public class Progress
{
public static void main(String[] args)
{
final int max = 100;
final JFrame frame = new JFrame("JProgress Demo");
// this would create a progress bar
final JProgressBar jp = new JProgressBar();
jp.setMinimum(0);
jp.setMaximum(max);
jp.setStringPainted(true);
// this would add a progress bar
frame.setLayout(new FlowLayout());
frame.getContentPane().add(jp);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
// this would be updating the progressbar
for (int i = 0; i <= max; i++)
{
final int presentValue = i;
try
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
jp.setValue(presentValue);
}
});
java.lang.Thread.sleep(100);
}
catch (InterruptedException e)
{
JOptionPane.showMessageDialog(frame, e.getMessage());
}
}
}
}
Output:
`
Example #2 – Vertical Progress bar
Code:
// Program for creation of the vertical progress bar
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class progress extends JFrame
{
// creation of the frame
static JFrame frame;
static JProgressBar bar;
public static void main(String[] args)
{
// create a frame
frame = new JFrame("ProgresBar demo");
// creation of the panel
JPanel panel = new JPanel();
// creation of the progressbar
bar = new JProgressBar(SwingConstants.VERTICAL);
// setting initial value
bar.setValue(0);
bar.setStringPainted(true);
// addition of progressbar
panel.add(bar);
// addition of panel
frame.add(panel);
// setting the size of the frame
frame.setSize(500, 500);
frame.setVisible(true);
fill();
}
// function that increases the progress
public static void fill()
{
int j = 0;
try {
while (j <= 100) {
// filling the menu bar
bar.setValue(j + 10);
// delaying the thread
Thread.sleep(1000);
j += 20;
}
}
catch (Exception e) {
}
}
}
Output:
Example #3 – Progress bar with string
Code:
import java.awt.*;
import javax.swing.*;
public class Jprogress extends JFrame {
// creation of the frame
static JFrame frame;
static JProgressBar bar;
public static void main(String[] args)
{
// creation of the frame
frame = new JFrame("ProgressBar demo");
// creation of the panel
JPanel panel = new JPanel();
// creation of the progressbar
bar = new JProgressBar();
// setting initial value
bar.setValue(0);
bar.setStringPainted(true);
// adding progressbar
panel.add(bar);
// adding panel
frame.add(panel);
// set the size of the frame
frame.setSize(500, 500);
frame.setVisible(true);
fill();
}
// function needed to increase progress
public static void fill()
{
int j = 0;
try {
while (j <= 100) {
// set text accoring to the level to which the bar is filled
if (j > 30 && j < 70)
bar.setString("wait for few soconds");
else if (j > 70)
bar.setString("almost done loading");
else
bar.setString("loading initiated");
// filling of the menu bar
bar.setValue(j + 10);
// delaying the thread
Thread.sleep(3000);
j += 20;
}
}
catch (Exception e) {
}
}
}
Output:
Conclusion
Thus we can conclude that the progress bar is the key UI element that is needed majorly so as to give feedback to the desired user. The user often doesn’t interact along with the progress bar. The JProgress bar is usually shown whenever the application gets busy, and also it is an indication to the user that the application is performing the task, and it is not frozen.
Recommended Articles
This is a guide to JProgressBar example. Here we discuss the constructors, methods of JProgressBar along with the examples and code implementation. You may also look at the following articles to learn more –