Progress Monitor in Java
Submitted by donbermoy on Friday, December 5, 2014 - 21:26.
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:
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.
4. Initialize your variables of the components in your Main.
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.
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.
Now, start the timer.
Put the ActionListener variable to our created button.
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.
Output:
Here's the full code of this tutorial:
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
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- static int progress;
- progress = 0;
- if (monitor != null) {
- if (timer == null) {
- if (monitor == null)
- return;
- if (monitor.isCanceled()) {
- timer.stop();
- } else {
- progress += 2;
- monitor.setProgress(progress);
- monitor.setNote("Loading: " + progress + "%");
- }
- }
- });
- }
- timer.start();
- progressButton.addActionListener(progressButtonActionListener);
- frame.getContentPane().add(progressButton);
- frame.setSize(300, 200);
- frame.setVisible(true);

- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class progressMonitor{
- static int progress;
- progress = 0;
- if (monitor != null) {
- if (timer == null) {
- if (monitor == null)
- return;
- if (monitor.isCanceled()) {
- timer.stop();
- } else {
- progress += 2;
- monitor.setProgress(progress);
- monitor.setNote("Loading: " + progress + "%");
- }
- }
- });
- }
- timer.start();
- }
- }
- };
- progressButton.addActionListener(progressButtonActionListener);
- frame.getContentPane().add(progressButton);
- frame.setSize(300, 200);
- frame.setVisible(true);
- }
- }
Add new comment
- 166 views