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

JSlider Component in Java

Babs

Zero-Day Exploit Developer
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
92
Likes
187
Bits
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:
  1. import

    javax.swing.*

    ;

    // this is used to access the JFrame and JSlider class
  2. 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.

  1. JFrame

    frame =

    new

    JFrame

    (

    "JSlider Component"

    )

    ;
  2. 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:

  1. slider.addChangeListener

    (

    new

    ChangeListener

    (

    )

    {
  2. public

    void

    stateChanged(

    ChangeEvent

    event)

    {
  3. int

    value =

    slider.getValue

    (

    )

    ;
  4. if

    (

    value ==

    0

    )

    {
  5. System

    .out

    .println

    (

    "Minimum value! -- The value is 0."

    )

    ;
  6. }

    else

    if

    (

    value >

    0

    &&

    value <=

    100

    )

    {
  7. System

    .out

    .println

    (

    "Normal value! -- The value is from 1 to 100"

    )

    ;
  8. }

    else

    if

    (

    value >

    100

    &&

    value <

    199

    )

    {
  9. System

    .out

    .println

    (

    "Above average value! -- The value is from 101 to 199"

    )

    ;
  10. }

    else

    {
  11. System

    .out

    .println

    (

    "Maximum value! -- The value is 200"

    )

    ;
  12. }
  13. }
  14. }

    )

    ;

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.

  1. frame.getContentPane

    (

    )

    .add

    (

    slider)

    ;
  2. frame.pack

    (

    )

    ;
  3. frame.setVisible

    (

    true

    )

    ;
  4. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;


Output:




Here's the full source code of this tutorial:

  1. import

    javax.swing.*

    ;

    // this is used to access the JFrame and JSlider class
  2. import

    javax.swing.event.*

    ;

    // as we will use the event for JSlider, we will use this package to access the ChangeEvent and ChangeListener


  3. public

    class

    jSliderComponent {

  4. public

    static

    void

    main(

    String

    [

    ]

    args)

    {
  5. JFrame

    frame =

    new

    JFrame

    (

    "JSlider Component"

    )

    ;
  6. final

    JSlider

    slider =

    new

    JSlider

    (

    JSlider

    .HORIZONTAL

    ,0

    , 200

    , 0

    )

    ;

  7. slider.addChangeListener

    (

    new

    ChangeListener

    (

    )

    {
  8. public

    void

    stateChanged(

    ChangeEvent

    event)

    {
  9. int

    value =

    slider.getValue

    (

    )

    ;
  10. if

    (

    value ==

    0

    )

    {
  11. System

    .out

    .println

    (

    "Minimum value! -- The value is 0."

    )

    ;
  12. }

    else

    if

    (

    value >

    0

    &&

    value <=

    100

    )

    {
  13. System

    .out

    .println

    (

    "Normal value! -- The value is from 1 to 100"

    )

    ;
  14. }

    else

    if

    (

    value >

    100

    &&

    value <

    199

    )

    {
  15. System

    .out

    .println

    (

    "Above average value! -- The value is from 101 to 199"

    )

    ;
  16. }

    else

    {
  17. System

    .out

    .println

    (

    "Maximum value! -- The value is 200"

    )

    ;
  18. }
  19. }
  20. }

    )

    ;

  21. frame.getContentPane

    (

    )

    .add

    (

    slider)

    ;
  22. frame.pack

    (

    )

    ;
  23. frame.setVisible

    (

    true

    )

    ;
  24. frame.setDefaultCloseOperation

    (

    JFrame

    .EXIT_ON_CLOSE

    )

    ;
  25. }
  26. }

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.
 

452,292

323,341

323,350

Top