ProgressBar Component in Java

Today in Java, i will teach you how to create a progressbar component using JProgressBar control in Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of ProgressBar.java. 2. Import javax.swing.* package because we will going to have the JProgressBar, JFrame, SwingUtilities in swing and also the JPanel as the container of this.
  1. import javax.swing.*;
3. Initialize a variable for JProgressBa and named it as pbar. This will be below in your public class ProgressBar extends JPanel.
  1. public class ProgressBar extends JPanel {
4. Create a constructor that is the same with the filename that will instantiate the JProgressBar, sets the minimum and maximum value of the progressbar, and will add this component in the JPanel.
  1. public ProgressBar() {
  2. pbar = new JProgressBar();
  3. pbar.setMinimum(0);
  4. pbar.setMaximum(100);
  5. add(pbar);
  6. }
5. Create also a method named updateBar and has variable newValue as Integer because this will be used to update the progress of the bar using the loop.
  1. public void updateBar(int newValue) {
  2. pbar.setValue(newValue);
  3. }
6. In your Main, instantiate the ProgressBar constructor that has the new keyword and make it final.
  1. final ProgressBar it = new ProgressBar();
Set the title.
  1. JFrame frame = new JFrame("Progress Bar Component");
Set the windows characteristics.
  1. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  2. frame.setContentPane(it);
  3. frame.pack();
  4. frame.setVisible(true);
  5. frame.setLocation(300,250);
  6. frame.setSize(200,150);
Create a For Loop statement that the minimum value is equal to 0 and maximum value is 100 that has an increment capability with an i variable as integer. Initialize percent variable as integer that will be equal to i with the final keyword.
  1. for (int i = 0; i <= 100; i++) {
  2. final int percent = i;
Now, create a try and catch method. In the try method, update the progressbar with it variable name and its value of for loop in percent variable using run method of SwingUtilities. And have a delay value of 100 millisecond. Then in the catch method, preferred to use the InterruptedException.
  1. try {
  2. SwingUtilities.invokeLater(new Runnable() {
  3. public void run() {
  4. it.updateBar(percent);
  5. }
  6. });
  7. java.lang.Thread.sleep(100);
  8. } catch (InterruptedException e) {
  9. ;
  10. }
Press F5 to run the program. Output: output Full source code:
  1. import javax.swing.*;
  2.  
  3. public class ProgressBar extends JPanel {
  4.  
  5.  
  6.  
  7. public ProgressBar() {
  8. pbar = new JProgressBar();
  9. pbar.setMinimum(0);
  10. pbar.setMaximum(100);
  11. add(pbar);
  12. }
  13.  
  14. public void updateBar(int newValue) {
  15. pbar.setValue(newValue);
  16. }
  17.  
  18. public static void main(String args[]) {
  19.  
  20. final ProgressBar it = new ProgressBar();
  21.  
  22. JFrame frame = new JFrame("Progress Bar Component");
  23. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  24. frame.setContentPane(it);
  25. frame.pack();
  26. frame.setVisible(true);
  27. frame.setLocation(300,250);
  28. frame.setSize(200,150);
  29.  
  30.  
  31. for (int i = 0; i <= 100; i++) {
  32. final int percent = i;
  33. try {
  34. SwingUtilities.invokeLater(new Runnable() {
  35. public void run() {
  36. it.updateBar(percent);
  37. }
  38. });
  39. java.lang.Thread.sleep(100);
  40. } catch (InterruptedException e) {
  41. ;
  42. }
  43. }
  44. }
  45. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Comments

Add new comment