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

Progress Monitor in Java

slinkcube

System Patcher
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
153
Likes
197
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
This tutorial will teach you how to create a progress monitor in java. A ProgressMonitor class simply monitors a progress in any operation. Hence, we will not use the JProgressBar component here.

So, now let's start this tutorial!


1. Open JCreator or NetBeans and make a java program with a file name of progressMonitor.java.

2. Import the following package library:
  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;
  3. import

    java.awt.event.*

    ;

3. Declare the static global variables. To access the progress monitor in java, we will use the ProgressMonitor class. Have also the Timer and an Integer variable named progress.
  1. static

    ProgressMonitor

    monitor;
  2. static

    int

    progress;
  3. static

    Timer

    timer;

4. Initialize your variables of the components in your Main.
  1. JFrame

    frame =

    new

    JFrame

    (

    "Progress Monitor"

    )

    ;
  2. JButton

    progressButton =

    new

    JButton

    (

    "Click This!"

    )

    ;

5. Now, we will create an ActionListener named progressButtonActionListener to perform the action when clicking the progressButton variable.

Here we will use the Component class to get the component which will perform the action and will use the variable monitor of the ProgressMonitor. We will indicate 0 as the starting number and 100 as our maximum number. And have your variable progress initializes to 0.
  1. ActionListener

    progressButtonActionListener =

    new

    ActionListener

    (

    )

    {
  2. public

    void

    actionPerformed(

    ActionEvent

    actionEvent)

    {
  3. Component

    parent =

    (

    Component

    )

    actionEvent.getSource

    (

    )

    ;
  4. monitor =

    new

    ProgressMonitor

    (

    parent, "Loading Progress"

    , "Getting Started..."

    , 0

    , 100

    )

    ;
  5. progress =

    0

    ;

We can have our Progress Monitor if it is not null. Then insert inside if the timer is null put then again that the monitor variable will return its value. And if clicking the button Cancel it will display Monitor canceled. Otherwise the progress will continue its load that will increment its progress by 2 (by putting inside the progress variable in the monitor variable of Progress Monitor). Then it will have its note of Loading its percentage by incrementing it to 2 and will stop at 100 as we declared that above.
  1. if

    (

    monitor !=

    null

    )

    {
  2. if

    (

    timer ==

    null

    )

    {
  3. timer =

    new

    Timer

    (

    250

    , new

    ActionListener

    (

    )

    {
  4. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  5. if

    (

    monitor ==

    null

    )
  6. return

    ;
  7. if

    (

    monitor.isCanceled

    (

    )

    )

    {
  8. System

    .out

    .println

    (

    "Monitor canceled"

    )

    ;
  9. timer.stop

    (

    )

    ;
  10. }

    else

    {
  11. progress +=

    2

    ;
  12. monitor.setProgress

    (

    progress)

    ;
  13. monitor.setNote

    (

    "Loading: "

    +

    progress +

    "%"

    )

    ;
  14. }
  15. }
  16. }

    )

    ;
  17. }

Now, start the timer.
  1. timer.start

    (

    )

    ;

Put the ActionListener variable to our created button.
  1. progressButton.addActionListener

    (

    progressButtonActionListener)

    ;

6. Add the button using the add method of the frame. Set its layout to GridLayout using the setLayout method. Lastly, set the size, visibility, and have its close operation of the frame.
  1. frame.getContentPane

    (

    )

    .add

    (

    progressButton)

    ;
  2. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    0

    , 1

    )

    )

    ;
  3. frame.setSize

    (

    300

    , 200

    )

    ;
  4. frame.setVisible

    (

    true

    )

    ;
  5. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;


Output:

progress.png


Here's the full code of this tutorial:

  1. import

    javax.swing.*

    ;
  2. import

    java.awt.*

    ;
  3. import

    java.awt.event.*

    ;

  4. public

    class

    progressMonitor{

  5. static

    ProgressMonitor

    monitor;
  6. static

    int

    progress;
  7. static

    Timer

    timer;

  8. public

    static

    void

    main(

    String

    args[

    ]

    )

    {
  9. JFrame

    frame =

    new

    JFrame

    (

    "Progress Monitor"

    )

    ;
  10. JButton

    progressButton =

    new

    JButton

    (

    "Click This!"

    )

    ;

  11. ActionListener

    progressButtonActionListener =

    new

    ActionListener

    (

    )

    {
  12. public

    void

    actionPerformed(

    ActionEvent

    actionEvent)

    {
  13. Component

    parent =

    (

    Component

    )

    actionEvent.getSource

    (

    )

    ;
  14. monitor =

    new

    ProgressMonitor

    (

    parent, "Loading Progress"

    , "Getting Started..."

    , 0

    , 100

    )

    ;
  15. progress =

    0

    ;
  16. if

    (

    monitor !=

    null

    )

    {
  17. if

    (

    timer ==

    null

    )

    {
  18. timer =

    new

    Timer

    (

    250

    , new

    ActionListener

    (

    )

    {
  19. public

    void

    actionPerformed(

    ActionEvent

    e)

    {
  20. if

    (

    monitor ==

    null

    )
  21. return

    ;
  22. if

    (

    monitor.isCanceled

    (

    )

    )

    {
  23. System

    .out

    .println

    (

    "Monitor canceled"

    )

    ;
  24. timer.stop

    (

    )

    ;
  25. }

    else

    {
  26. progress +=

    2

    ;
  27. monitor.setProgress

    (

    progress)

    ;
  28. monitor.setNote

    (

    "Loading: "

    +

    progress +

    "%"

    )

    ;
  29. }
  30. }
  31. }

    )

    ;
  32. }
  33. timer.start

    (

    )

    ;
  34. }
  35. }
  36. }

    ;


  37. progressButton.addActionListener

    (

    progressButtonActionListener)

    ;

  38. frame.getContentPane

    (

    )

    .add

    (

    progressButton)

    ;
  39. frame.getContentPane

    (

    )

    .setLayout

    (

    new

    GridLayout

    (

    0

    , 1

    )

    )

    ;
  40. frame.setSize

    (

    300

    , 200

    )

    ;
  41. frame.setVisible

    (

    true

    )

    ;
  42. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  43. }
  44. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
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.
 

450,632

323,159

323,168

Top