slinkcube
System Patcher
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:
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
Download
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:
- import
javax.swing.*
;
- import
java.awt.*
;
- 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.
- static
ProgressMonitor
monitor;
- static
int
progress;
- static
Timer
timer;
4. Initialize your variables of the components in your Main.
- JFrame
frame =
new
JFrame
(
"Progress Monitor"
)
;
- 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.
- ActionListener
progressButtonActionListener =
new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
actionEvent)
{
- Component
parent =
(
Component
)
actionEvent.getSource
(
)
;
- monitor =
new
ProgressMonitor
(
parent, "Loading Progress"
, "Getting Started..."
, 0
, 100
)
;
- 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.
- if
(
monitor !=
null
)
{
- if
(
timer ==
null
)
{
- timer =
new
Timer
(
250
, new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
e)
{
- if
(
monitor ==
null
)
- return
;
- if
(
monitor.isCanceled
(
)
)
{
- System
.out
.println
(
"Monitor canceled"
)
;
- timer.stop
(
)
;
- }
else
{
- progress +=
2
;
- monitor.setProgress
(
progress)
;
- monitor.setNote
(
"Loading: "
+
progress +
"%"
)
;
- }
- }
- }
)
;
- }
Now, start the timer.
- timer.start
(
)
;
Put the ActionListener variable to our created button.
- 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.
- frame.getContentPane
(
)
.add
(
progressButton)
;
- frame.getContentPane
(
)
.setLayout
(
new
GridLayout
(
0
, 1
)
)
;
- frame.setSize
(
300
, 200
)
;
- frame.setVisible
(
true
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
Output:

Here's the full code of this tutorial:
- import
javax.swing.*
;
- import
java.awt.*
;
- import
java.awt.event.*
;
- public
class
progressMonitor{
- static
ProgressMonitor
monitor;
- static
int
progress;
- static
Timer
timer;
- public
static
void
main(
String
args[
]
)
{
- JFrame
frame =
new
JFrame
(
"Progress Monitor"
)
;
- JButton
progressButton =
new
JButton
(
"Click This!"
)
;
- ActionListener
progressButtonActionListener =
new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
actionEvent)
{
- Component
parent =
(
Component
)
actionEvent.getSource
(
)
;
- monitor =
new
ProgressMonitor
(
parent, "Loading Progress"
, "Getting Started..."
, 0
, 100
)
;
- progress =
0
;
- if
(
monitor !=
null
)
{
- if
(
timer ==
null
)
{
- timer =
new
Timer
(
250
, new
ActionListener
(
)
{
- public
void
actionPerformed(
ActionEvent
e)
{
- if
(
monitor ==
null
)
- return
;
- if
(
monitor.isCanceled
(
)
)
{
- System
.out
.println
(
"Monitor canceled"
)
;
- timer.stop
(
)
;
- }
else
{
- progress +=
2
;
- monitor.setProgress
(
progress)
;
- monitor.setNote
(
"Loading: "
+
progress +
"%"
)
;
- }
- }
- }
)
;
- }
- timer.start
(
)
;
- }
- }
- }
;
- progressButton.addActionListener
(
progressButtonActionListener)
;
- frame.getContentPane
(
)
.add
(
progressButton)
;
- frame.getContentPane
(
)
.setLayout
(
new
GridLayout
(
0
, 1
)
)
;
- frame.setSize
(
300
, 200
)
;
- frame.setVisible
(
true
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- }
- }
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.