Showing posts with label what is jslider in java swing with example. Show all posts
Showing posts with label what is jslider in java swing with example. Show all posts

Monday, 8 April 2019

JSlider in java | Online Help

JSlider in java:

The JSlider class is used to select an integer value from a range of integer values. There are two types of sliders. They are:

-Horizontal slider

-Vertical slider


Horizontal slider:

The minimum integer value is present at the left extreme and the maximum value is at the extreme right.

Vertical slider:

The minimum integer value is present at the extreme bottom and the maximum value is at the extreme top.

The value which is pointed by thumb is selected.JSlider can be incremented or decremented by on side respectively.

Constructors available in JSlider in java:

The JSlider class has five constructors as follow:

JSlider()

This method is used to create a JSlider with a range of 0-100.

JSlider(int direction)

This method is used to create a JSlider with the specified direction.

JSlider(int min,int max)

This method is used to create a JSlider with a range of min to max value.

JSlider(int direction,int min,int max)

This method is used to create a JSlider with a range of min to max value and the specified direction.

JSlider(int direction,int min,int max,int value)

This method is used to create a JSlider with a range of min to max, an initial value as specified and the specified direction.

Parameters:

direction: Swing Constants.HORIZONTAL / Swing Constants.VERTICAL
min, max: Minimum and Maximum for the JSlider value.
value: Initial value for the JSlider

Methods for JSlider in java:

The most commonly used JSlider() methods are as follow:

void setMajorTickSpacing( int units)

The setMajorTickSpacing()method is used to set the spacing of major tick marks.

void setMinorTickSpacing(int units)

The setMinorTickSpacing()method is used to set the spacing of minor tick marks.

void setPaintTicks(Boolean b)

The setPaintTicks() method is used to display the tick marks. If it is true. By default, it is not shown.

Sample Program for JSlider in java:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
/*<applet code="jsliderdemo" width=500 height=500></applet>*/
public class jsliderdemo extends JApplet implements ChangeListener
{
JSlider js=new JSlider(SwingConstants.HORIZONTAL,0,100,0);
public void init()
{
Container c=getContentPane();
c.setLayout(new FlowLayout());
js.addChangeListener(this);
c.add(js);
}
public void stateChanged(ChaneEvent e)
{
JSlider js1=(JSlider)e.getSource();
showStatus("slider minimum:"+js1.getMinimum()+",maximum:"+js1.getMaximum+",value:"+js1getValue());
}
}