This is a Java Program to Progress a Progress Bar by 5 Units on Every Click of Push Button
We have to write a program in Java such that it creates a Progress Bar and a Push Button. Every time the push button is clicked the progress bar is progressed by 5 units.
For progressing the progress bar, we can have the following different sets of input and output.
1. To Display the Progress Bar:
When the program is being executed, it is expected that a progress bar is created with initial value zero.
2. To Progress the Progress Bar:
Every time the push button is clicked, it is expected that the progress bar progresses by 5 units.
1. Create a frame containing a progress bar and push button.
2. Set the initial value of the progress bar to zero.
3. Every time the push button is clicked, increase the progress bar by 5 units.
Here is source code of the Java Program to progress the progress bar. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/* Java Program to progress the progress bar*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Progress_Bar implements ActionListener
{
static JProgressBar bar;
//Driver function
public static void main(String args[])
{
//Create a frame
JFrame frame = new JFrame("Progress Bar");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
frame.setLayout(null);
//Create an object
Progress_Bar obj = new Progress_Bar();
//Create a Progress Bar
bar = new JProgressBar();
bar.setValue(0);
bar.setStringPainted(true);
bar.setBounds(150,100,200,50);
frame.add(bar);
//Create a button
JButton increase = new JButton("Increase");
increase.setBounds(200,250,100,50);
increase.addActionListener(obj);
frame.add(increase);
//Display the frame
frame.setVisible(true);
}
//Function to progress the progress bar
public void actionPerformed(ActionEvent e)
{
bar.setValue(bar.getValue()+5);
}
}
1. Create a progress bar using JProgressBar class.
2. To set the value of the progress bar use setValue function.
3. To get the present value of progress bar use getValue function.
4. To display the value of the progress bar within the bar use setStringPainted(true).
Here’s the run time test case to show the progress the progress bar.
Test case 1 – To View the Progress Bar.
Test case 2 – To Progress the Progress Bar.
Sanfoundry Global Education & Learning Series – Java Programs.
- Apply for Computer Science Internship
- Practice Information Technology MCQs
- Practice BCA MCQs
- Apply for Java Internship
- Check Java Books