Babs
Zero-Day Exploit Developer
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
200 XP
This is a tutorial in which we will going to create a program that has the JSlider Component using Java. The JSlider is used to let the user easily enter or slide a numeric value bounded by a minimum and maximum value.
So, now let's start this tutorial!
1. Open JCreator or NetBeans and make a java program with a file name of jSliderComponent.java.
2. Import the following packages:
3. Initialize your variable in your Main, variable frame for creating JFrame and variable slider for the JSlider component.
As you noticed the above code, we have declared the JSlider into final because we will going to create a class inside of it. The JSlider class has these four parameters to be instantiated, the JSlider(int orientation, int min, int max, int value). There are two orientations either JSlider.HORIZONTAL or JSlider.VERTICAL. The last three int arguments, when present, specify the slider's minimum, maximum, and initial values. The value 0 is the minimum value, 200 is the maximum value, and the 0 again is the starting value.
4. Now, we will create a Change Event and the Change Listener in our slider. This will trigger to guide the value changed by the user in the slider. Have this code below:
We have the stateChanged method in our Change Listener and initialize a variable value as integer that is equal to the value of the slider. Then we have created an if-else statement to recognize the value of the slider. If the value is set to 0, then it will display "Minimum value! -- The value is 0.". If the value is from 1 to 100, then it will display "Normal value! -- The value is from 1 to 100". If the value is from 101 to 199, it will display ""Above average value! -- The value is from 101 to 199". If the value is 200, then it will display "Maximum value! -- The value is 200".
5. Add the JSlider to the frame using the add method, then pack the frame so that the component/control will be seen directly to the frame. Also set its visibility to true and have it the close operation to exit.
Output:
Here's the full source code of this tutorial:
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number or Facebook 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 jSliderComponent.java.
2. Import the following packages:
- import
javax.swing.*
;
// this is used to access the JFrame and JSlider class
- import
javax.swing.event.*
;
// as we will use the event for JSlider, we will use this package to access the ChangeEvent and ChangeListener
3. Initialize your variable in your Main, variable frame for creating JFrame and variable slider for the JSlider component.
- JFrame
frame =
new
JFrame
(
"JSlider Component"
)
;
- final
JSlider
slider =
new
JSlider
(
JSlider
.HORIZONTAL
,0
, 200
, 0
)
;
As you noticed the above code, we have declared the JSlider into final because we will going to create a class inside of it. The JSlider class has these four parameters to be instantiated, the JSlider(int orientation, int min, int max, int value). There are two orientations either JSlider.HORIZONTAL or JSlider.VERTICAL. The last three int arguments, when present, specify the slider's minimum, maximum, and initial values. The value 0 is the minimum value, 200 is the maximum value, and the 0 again is the starting value.
4. Now, we will create a Change Event and the Change Listener in our slider. This will trigger to guide the value changed by the user in the slider. Have this code below:
- slider.addChangeListener
(
new
ChangeListener
(
)
{
- public
void
stateChanged(
ChangeEvent
event)
{
- int
value =
slider.getValue
(
)
;
- if
(
value ==
0
)
{
- System
.out
.println
(
"Minimum value! -- The value is 0."
)
;
- }
else
if
(
value >
0
&&
value <=
100
)
{
- System
.out
.println
(
"Normal value! -- The value is from 1 to 100"
)
;
- }
else
if
(
value >
100
&&
value <
199
)
{
- System
.out
.println
(
"Above average value! -- The value is from 101 to 199"
)
;
- }
else
{
- System
.out
.println
(
"Maximum value! -- The value is 200"
)
;
- }
- }
- }
)
;
We have the stateChanged method in our Change Listener and initialize a variable value as integer that is equal to the value of the slider. Then we have created an if-else statement to recognize the value of the slider. If the value is set to 0, then it will display "Minimum value! -- The value is 0.". If the value is from 1 to 100, then it will display "Normal value! -- The value is from 1 to 100". If the value is from 101 to 199, it will display ""Above average value! -- The value is from 101 to 199". If the value is 200, then it will display "Maximum value! -- The value is 200".
5. Add the JSlider to the frame using the add method, then pack the frame so that the component/control will be seen directly to the frame. Also set its visibility to true and have it the close operation to exit.
- frame.getContentPane
(
)
.add
(
slider)
;
- frame.pack
(
)
;
- frame.setVisible
(
true
)
;
- frame.setDefaultCloseOperation
(
JFrame
.EXIT_ON_CLOSE
)
;
Output:
Loading…
www.sourcecodester.com
Here's the full source code of this tutorial:
- import
javax.swing.*
;
// this is used to access the JFrame and JSlider class
- import
javax.swing.event.*
;
// as we will use the event for JSlider, we will use this package to access the ChangeEvent and ChangeListener
- public
class
jSliderComponent {
- public
static
void
main(
String
[
]
args)
{
- JFrame
frame =
new
JFrame
(
"JSlider Component"
)
;
- final
JSlider
slider =
new
JSlider
(
JSlider
.HORIZONTAL
,0
, 200
, 0
)
;
- slider.addChangeListener
(
new
ChangeListener
(
)
{
- public
void
stateChanged(
ChangeEvent
event)
{
- int
value =
slider.getValue
(
)
;
- if
(
value ==
0
)
{
- System
.out
.println
(
"Minimum value! -- The value is 0."
)
;
- }
else
if
(
value >
0
&&
value <=
100
)
{
- System
.out
.println
(
"Normal value! -- The value is from 1 to 100"
)
;
- }
else
if
(
value >
100
&&
value <
199
)
{
- System
.out
.println
(
"Above average value! -- The value is from 101 to 199"
)
;
- }
else
{
- System
.out
.println
(
"Maximum value! -- The value is 200"
)
;
- }
- }
- }
)
;
- frame.getContentPane
(
)
.add
(
slider)
;
- frame.pack
(
)
;
- 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 or Facebook 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.