Java Program to Progress a Progress Bar by 5 Units on Every Click

This is a Java Program to Progress a Progress Bar by 5 Units on Every Click of Push Button

Problem Description

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.

Expected Input and Output

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:

advertisement
advertisement
Every time the push button is clicked,
it is expected that the progress bar progresses by 5 units.
Problem Solution

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.

Program/Source Code

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.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /* Java Program to progress the progress bar*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Progress_Bar implements ActionListener
  6. {
  7.     static JProgressBar bar;
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11. 	//Create a frame
  12. 	JFrame frame = new JFrame("Progress Bar");
  13. 	frame.setSize(500,500);
  14. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	frame.getContentPane().setBackground(Color.white);
  16. 	frame.setLayout(null);
  17. 	//Create an object
  18. 	Progress_Bar obj = new Progress_Bar();
  19. 	//Create a Progress Bar
  20. 	bar = new JProgressBar();
  21. 	bar.setValue(0);
  22. 	bar.setStringPainted(true);
  23. 	bar.setBounds(150,100,200,50);
  24. 	frame.add(bar);
  25. 	//Create a button
  26. 	JButton increase = new JButton("Increase");
  27. 	increase.setBounds(200,250,100,50);
  28. 	increase.addActionListener(obj);
  29. 	frame.add(increase);
  30. 	//Display the frame
  31. 	frame.setVisible(true);
  32.     }
  33.     //Function to progress the progress bar
  34.     public void actionPerformed(ActionEvent e)
  35.     {
  36.         bar.setValue(bar.getValue()+5);
  37.     }
  38. }
Program Explanation

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).

Runtime Test Cases

Here’s the run time test case to show the progress the progress bar.

advertisement

Test case 1 – To View the Progress Bar.
java-program-progress-bar-view

Test case 2 – To Progress the Progress Bar.
java-program-progress-bar-increase-5

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.