JSlider Component in Java

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: http://www.sourcecodester.com/sites/default/files/jslider_0.png 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.  
  4.  
  5. public class jSliderComponent {
  6.        
  7.   public static void main(String[] args) {
  8.     JFrame frame = new JFrame("JSlider Component");
  9.     final JSlider slider = new JSlider(JSlider.HORIZONTAL,0, 200, 0);
  10.    
  11.     slider.addChangeListener(new ChangeListener() {
  12.       public void stateChanged(ChangeEvent event) {
  13.         int value = slider.getValue();
  14.         if (value == 0) {
  15.                 System.out.println("Minimum value! -- The value is 0.");
  16.         } else if (value > 0 && value <= 100) {
  17.            System.out.println("Normal value! -- The value is from 1 to 100");
  18.         } else if (value > 100 && value < 199) {
  19.            System.out.println("Above average value! -- The value is from 101 to 199");
  20.         } else {
  21.             System.out.println("Maximum value! -- The value is 200");
  22.         }
  23.       }
  24.     });
  25.    
  26.     frame.getContentPane().add(slider);
  27.     frame.pack();
  28.     frame.setVisible(true);
  29.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30.   }
  31. }
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

Add new comment