4 Ways to Create a JSlider - Swing

Screenshot of JSlider Example
Here are 4 simple ways to create a JSlider in Swing. The JSlider class is big, there is no doubt in that. It has got about 6 constructors (here I'll explain 5 of them which I'll list here).

Constructors


JSlider()
JSlider(int orientation)
JSlider(int min,int max)
JSlider(int min,int max,int val)
JSlider(int orientation,int min,int max,int val)

Methods


public void setExtent(int extent) // value+extent is maximum value to which a jslider can be set to. See constraints below.

public void setFont(Font f) // font for labels

public void setInverted(boolean flag) // slide from the inverted direction

public void setLabelTable(Dictionary d) // labels dictionary containing value-label pairs


public void setMajorTickSpacing(int space) // value between big ticks

public void setMinorTickSpacing(int space) // value between small ticks


public void setMinimum(int min) // minimum value for jslider

public void setMaximum(int max) // maximum value for jslider


public void setOrientation(int orientation) // Set orientation horizontal/vertical

public void setSnapToTicks(boolean b) // Auto adjust JSlider to nearest tick when value is changed


public void setPaintLabels(boolean b) // Paint labels

public void setPaintTicks(boolean b) // Paint tick (a tick is a line that corresponds a value)

Constraints: You need to know the constraints to understand what an extent means.

minimum <= value <= value+extent <= maximum


import javax.swing.*;
import java.awt.*;
import java.util.*;
class JSliderExample extends JFrame
{
JSlider s1,s2,s3,s4,s5;
Hashtable<Integer,JLabel> h=new Hashtable<>();
   
    {
    h.put(0,new JLabel("Zero"));
    h.put(10,new JLabel("Ten"));
    h.put(20,new JLabel("Twenty"));
    h.put(30,new JLabel("Thirty"));
    h.put(40,new JLabel("Forty"));
    h.put(50,new JLabel("Fifty"));
    // This is not taken, see down since
    // s4 maximum val is 50
    // h.put(60,new JLabel("Sixty"));
    }

    public JSliderExample()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("JSlider Example");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());
      
        s1=new JSlider();
        // orientation
        s2=new JSlider(SwingConstants.VERTICAL);
        // min,max
        s3=new JSlider(10,90);
        // min,max,val
        s4=new JSlider(0,50,10);
        // orientation, min,max,val
        s5=new JSlider(SwingConstants.HORIZONTAL,0,100,20);
      
        // Set value
        s1.setValue(20);
        s2.setValue(40);
        s3.setValue(20);
      
        // Set minimum,maximum
        s1.setMinimum(10);
        s1.setMaximum(40);
      
        // If you set more than maximum, then
        // maximum is considered, i.e.
        // here setValue(40) is substituted
        s1.setValue(42);
      
        System.out.println("Value of s1 is "+s1.getValue());
      
        // Set orientation
        s4.setOrientation(SwingConstants.VERTICAL);
      
        // Set border
        s1.setBorder(BorderFactory.createLineBorder(Color.GRAY,1,false));
      
        s1.setMinorTickSpacing(5);
        s1.setMajorTickSpacing(10);
        s4.setMajorTickSpacing(10);
        s5.setMinorTickSpacing(5);
        s5.setMajorTickSpacing(10);
      
        // Set paint labels
        s1.setPaintLabels(true);
        s1.setPaintTicks(true);
        s4.setPaintLabels(true);
        s5.setPaintLabels(true);
        s5.setPaintTicks(true);
      
        s1.setSnapToTicks(true);
      
        s1.setFont(new Font("Tahoma",Font.PLAIN,11));
      
        s3.setInverted(true);
      
      
        s4.setLabelTable(h);
      
        // This doesn't work because, in label table, separate
        // JLabel objects are acting as JLabels
        // s4.setFont(new Font("Tahoma",Font.PLAIN,11));
      
      
        // Now though the max is 100, user can
        // drag slider upto 100-20=80 only
        // i.e. (max-setExtent)=max_val
        s5.setExtent(20);
      
        add(s1);
        add(s2);
        add(s3);
        add(s4);
        add(s5);
      
        setSize(600,400);
        setVisible(true);
    }
   
    public static void main(String args[])
    {
        new JSliderExample();
    }
}

Also see combining JSlider and JMenu and increase JTextArea font size with JSlider

The greatest compliment you can give me is when you share this with others. I sincerely appreciate it :)

No comments: