• We just launched and are currently in beta. Join us as we build and grow the community.

ProgressBar Component in Java

a11th3w4y

Privilege Escalation Expert
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
156
Likes
28
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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

    {
  2. JProgressBar

    pbar;

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:
progressbarout.png


Full source code:

  1. import

    javax.swing.*

    ;

  2. public

    class

    ProgressBar extends

    JPanel

    {

  3. JProgressBar

    pbar;


  4. public

    ProgressBar(

    )

    {
  5. pbar =

    new

    JProgressBar

    (

    )

    ;
  6. pbar.setMinimum

    (

    0

    )

    ;
  7. pbar.setMaximum

    (

    100

    )

    ;
  8. add(

    pbar)

    ;
  9. }

  10. public

    void

    updateBar(

    int

    newValue)

    {
  11. pbar.setValue

    (

    newValue)

    ;
  12. }

  13. public

    static

    void

    main(

    String

    args[

    ]

    )

    {

  14. final

    ProgressBar it =

    new

    ProgressBar(

    )

    ;

  15. JFrame

    frame =

    new

    JFrame

    (

    "Progress Bar Component"

    )

    ;
  16. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  17. frame.setContentPane

    (

    it)

    ;
  18. frame.pack

    (

    )

    ;
  19. frame.setVisible

    (

    true

    )

    ;
  20. frame.setLocation

    (

    300

    ,250

    )

    ;
  21. frame.setSize

    (

    200

    ,150

    )

    ;


  22. for

    (

    int

    i =

    0

    ;

    i <=

    100

    ;

    i++

    )

    {
  23. final

    int

    percent =

    i;
  24. try

    {
  25. SwingUtilities

    .invokeLater

    (

    new

    Runnable

    (

    )

    {
  26. public

    void

    run(

    )

    {
  27. it.updateBar

    (

    percent)

    ;
  28. }
  29. }

    )

    ;
  30. java.lang

    .Thread

    .sleep

    (

    100

    )

    ;
  31. }

    catch

    (

    InterruptedException

    e)

    {
  32. ;
  33. }
  34. }
  35. }
  36. }

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


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top