|
How to use a slider with tick marks and labels |
|
|
This Java Swing tip illustrates a method of implementing a slider with tick
marks and labels. The JSlider used in tip used in a similar manner to Scrollbar:
the zero-argument constructor creates a horizontal slider with a range from 0
to 100 and an initial value of 50. You can also supply the orientation (via
JSlider.HORIZONTAL or JSlider.VERTICAL) and the range and initial value to the
constructor. Developer may handle events by attaching a ChangeListener. Its
stateChanged method normally calls getValue to look up the current JSlider value.
import java.awt.*;
import javax.swing.*;
public class JSliders extends JFrame {
public static void main(String[] args) {
new JSliders();
}
public JSliders() {
super("Using JSlider");
// Comment out next line for Java LAF
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
JSlider slider1 = new JSlider();
slider1.setBorder(BorderFactory.createTitledBorder(
"JSlider without Tick Marks"));
content.add(slider1, BorderLayout.NORTH);
JSlider slider2 = new JSlider();
slider2.setBorder(BorderFactory.createTitledBorder(
"JSlider with Tick Marks"));
slider2.setMajorTickSpacing(20);
slider2.setMinorTickSpacing(5);
slider2.setPaintTicks(true);
content.add(slider2, BorderLayout.CENTER);
JSlider slider3 = new JSlider();
slider3.setBorder(BorderFactory.createTitledBorder(
"JSlider with Tick Marks & Labels"));
slider3.setMajorTickSpacing(20);
slider3.setMinorTickSpacing(5);
slider3.setPaintTicks(true);
slider3.setPaintLabels(true);
content.add(slider3, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.