Bitcoin

Bitcoin
Bitcoin

JSlider Java

Introduction

The class JSlider is a component which lets the user graphically select a value by sliding a knob within a bounded interval.

Class Declaration

Following is the declaration for javax.swing.JSlider class −
public class JSlider
   extends JComponent
      implements SwingConstants, Accessible

Field

Following are the fields for javax.swing.JSlider class −
  • protected ChangeEvent changeEvent − Only one ChangeEvent is needed per slider instance since the event's only (read-only) state is the source property.
  • protected ChangeListener changeListener − The changeListener (no suffix) is the listener we add to the slider's model.
  • protected int majorTickSpacing − The number of values between the major tick marks -- the larger marks that break up the minor tick marks.
  • protected int minorTickSpacing − The number of values between the minor tick marks -- the smaller marks that occur between the major tick marks.
  • protected int orientation − Whether the slider is horizontal or vertical The default is horizontal.
  • protected BoundedRangeModel sliderModel − The data model that handles the numeric maximum value, minimum value, and current-position value for the slider.
  • protected boolean snapToTicks − If true, the knob (and the data value it represents) resolve to the closest tick mark next to where the user positioned the knob.

Class Constructors

Sr.No.Constructor & Description
1
JSlider()
Creates a horizontal slider with the range 0 to 100 and an initial value of 50.
2
JSlider(BoundedRangeModel brm)
Creates a horizontal slider using the specified BoundedRangeModel.
3
JSlider(int orientation)
Creates a slider using the specified orientation with the range 0 to 100 and an initial value of 50.
4
JSlider(int min, int max)
Creates a horizontal slider using the specified min and max with an initial value equal to the average of the min plus max.
5
JSlider(int min, int max, int value)
Creates a horizontal slider using the specified min, max and value.
6
JSlider(int orientation, int min, int max, int value)
Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.

Class Methods



Methods Inherited

This class inherits methods from the following classes −
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JSlider Example

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}
ANOTHER JSLIDER CODE
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class JSliderDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;

   public JSliderDemo(){
      prepareGUI();
   }
   public static void main(String[] args){
      JSliderDemo  jSliderDemo = new JSliderDemo();      
      jSliderDemo.showSliderDemo();
   }
   private void prepareGUI(){
      mainFrame = new JFrame("Java Swing Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new JLabel("", JLabel.CENTER);        
      statusLabel = new JLabel("",JLabel.CENTER);    
      statusLabel.setSize(350,100);

      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }
   private void showSliderDemo(){
      headerLabel.setText("Control in action: JSlider"); 
      JSlider slider = new JSlider(JSlider.HORIZONTAL,0,100,10);
      
      slider.addChangeListener(new ChangeListener() {
         public void stateChanged(ChangeEvent e) {
            statusLabel.setText("Value : " + ((JSlider)e.getSource()).getValue());
         }
      });
      controlPanel.add(slider);      
      mainFrame.setVisible(true);     
   } 
}
THE OUTPUT IS

No comments:

Post a Comment

Facebook