Monday, 14 September 2015

JSlider class

JSlider class

The JSlider is used to create the slider. By using JSlider a user can select a value from a specific range.

Commonly used Constructors of JSlider class

  • JSlider(): creates a slider with the initial value of 50 and range of 0 to 100.
  • JSlider(int orientation): creates a slider with the specified orientation set by either JSlider.HORIZONTAL or JSlider.VERTICAL with the range 0 to 100 and initial value 50.
  • JSlider(int min, int max): creates a horizontal slider using the given min and max.
  • JSlider(int min, int max, int value): creates a horizontal slider using the given min, max and value.
  • JSlider(int orientation, int min, int max, int value): creates a slider using the given orientation, min, max and value.

Commonly used Methods of JSlider class

1) public void setMinorTickSpacing(int n): is used to set the minor tick spacing to the slider.
2) public void setMajorTickSpacing(int n): is used to set the major tick spacing to the slider.
3) public void setPaintTicks(boolean b): is used to determine whether tick marks are painted.
4) public void setPaintLabels(boolean b): is used to determine whether labels are painted.
5) public void setPaintTracks(boolean b): is used to determine whether track is painted.

Simple example of JSlider class

example of JSlider class
  1. import javax.swing.*;  
  2.   
  3. public class SliderExample1 extends JFrame{  
  4.   
  5. public SliderExample1() {  
  6. JSlider slider = new JSlider(JSlider.HORIZONTAL, 05025);  
  7. JPanel panel=new JPanel();  
  8. panel.add(slider);  
  9.   
  10. add(panel);  
  11. }  
  12.   
  13. public static void main(String s[]) {  
  14. SliderExample1 frame=new SliderExample1();  
  15. frame.pack();  
  16. frame.setVisible(true);  
  17. }  

Example of JSlider class that paints ticks:

example of JSlider class
  1. import javax.swing.*;  
  2.   
  3. public class SliderExample extends JFrame{  
  4.   
  5. public SliderExample() {  
  6.   
  7. JSlider slider = new JSlider(JSlider.HORIZONTAL, 05025);  
  8. slider.setMinorTickSpacing(2);  
  9. slider.setMajorTickSpacing(10);  
  10.   
  11. slider.setPaintTicks(true);  
  12. slider.setPaintLabels(true);  
  13.   
  14. JPanel panel=new JPanel();  
  15. panel.add(slider);  
  16. add(panel);  
  17. }  
  18.   
  19. public static void main(String s[]) {  
  20. SliderExample frame=new SliderExample();  
  21. frame.pack();  
  22. frame.setVisible(true);  
  23.   
  24. }  
  25. }  

No comments:

Post a Comment