a11th3w4y
Privilege Escalation Expert
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.
3. Initialize a variable for JProgressBa and named it as pbar. This will be below in your 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.
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.
6. In your Main, instantiate the ProgressBar constructor that has the new keyword and make it final.
Set the title.
Set the windows characteristics.
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.
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.
Press F5 to run the program.
Output:
Full source code:
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
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.
- 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.
- public
class
ProgressBar extends
JPanel
{
- 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.
- public
ProgressBar(
)
{
- pbar =
new
JProgressBar
(
)
;
- pbar.setMinimum
(
0
)
;
- pbar.setMaximum
(
100
)
;
- add(
pbar)
;
- }
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.
- public
void
updateBar(
int
newValue)
{
- pbar.setValue
(
newValue)
;
- }
6. In your Main, instantiate the ProgressBar constructor that has the new keyword and make it final.
- final
ProgressBar it =
new
ProgressBar(
)
;
Set the title.
Set the windows characteristics.
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.setContentPane
(
it)
;
- frame.pack
(
)
;
- frame.setVisible
(
true
)
;
- frame.setLocation
(
300
,250
)
;
- 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.
- for
(
int
i =
0
;
i <=
100
;
i++
)
{
- 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.
- try
{
- SwingUtilities
.invokeLater
(
new
Runnable
(
)
{
- public
void
run(
)
{
- it.updateBar
(
percent)
;
- }
- }
)
;
- java.lang
.Thread
.sleep
(
100
)
;
- }
catch
(
InterruptedException
e)
{
- ;
- }
Press F5 to run the program.
Output:

Full source code:
- import
javax.swing.*
;
- public
class
ProgressBar extends
JPanel
{
- JProgressBar
pbar;
- public
ProgressBar(
)
{
- pbar =
new
JProgressBar
(
)
;
- pbar.setMinimum
(
0
)
;
- pbar.setMaximum
(
100
)
;
- add(
pbar)
;
- }
- public
void
updateBar(
int
newValue)
{
- pbar.setValue
(
newValue)
;
- }
- public
static
void
main(
String
args[
]
)
{
- final
ProgressBar it =
new
ProgressBar(
)
;
- JFrame
frame =
new
JFrame
(
"Progress Bar Component"
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
- frame.setContentPane
(
it)
;
- frame.pack
(
)
;
- frame.setVisible
(
true
)
;
- frame.setLocation
(
300
,250
)
;
- frame.setSize
(
200
,150
)
;
- for
(
int
i =
0
;
i <=
100
;
i++
)
{
- final
int
percent =
i;
- try
{
- SwingUtilities
.invokeLater
(
new
Runnable
(
)
{
- public
void
run(
)
{
- it.updateBar
(
percent)
;
- }
- }
)
;
- java.lang
.Thread
.sleep
(
100
)
;
- }
catch
(
InterruptedException
e)
{
- ;
- }
- }
- }
- }
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.