Sample Sliders : Slider « Swing JFC « Java

Sample Sliders

Sample Sliders
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Polygon;
import java.util.Hashtable;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;

public class SampleSliders {
  public static void main(String args[]) {
    String title = (args.length == 0 ? "Sample Slider" : args[0]);
    JFrame f = new JFrame(title);

    JSlider js1 = new JSlider();
    js1.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    JSlider js2 = new JSlider();
    JSlider js3 = new JSlider(JSlider.VERTICAL);
    JSlider js4 = new JSlider(JSlider.VERTICAL);
    Hashtable table = new Hashtable();
    table.put(new Integer(0), new JLabel(new DiamondIcon(;
    table.put(new Integer(10), new JLabel("Ten"));
    table.put(new Integer(25), new JLabel("Twenty-Five"));
    table.put(new Integer(34), new JLabel("Thirty-Four"));
    table.put(new Integer(52), new JLabel("Fifty-Two"));
    table.put(new Integer(70), new JLabel("Seventy"));
    table.put(new Integer(82), new JLabel("Eighty-Two"));
    table.put(new Integer(100), new JLabel(new DiamondIcon(;
    Container c = f.getContentPane();
    c.add(js1, BorderLayout.NORTH);
    c.add(js2, BorderLayout.SOUTH);
    c.add(js3, BorderLayout.WEST);
    c.add(js4, BorderLayout.EAST);
    f.setSize(300, 200);

class DiamondIcon implements Icon {
  private Color color;

  private boolean selected;

  private int width;

  private int height;

  private Polygon poly;

  private static final int DEFAULT_WIDTH = 10;

  private static final int DEFAULT_HEIGHT = 10;

  public DiamondIcon(Color color) {
    this(color, true, DEFAULT_WIDTH, DEFAULT_HEIGHT);

  public DiamondIcon(Color color, boolean selected) {
    this(color, selected, DEFAULT_WIDTH, DEFAULT_HEIGHT);

  public DiamondIcon(Color color, boolean selected, int width, int height) {
    this.color = color;
    this.selected = selected;
    this.width = width;
    this.height = height;

  private void initPolygon() {
    poly = new Polygon();
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    poly.addPoint(0, halfHeight);
    poly.addPoint(halfWidth, 0);
    poly.addPoint(width, halfHeight);
    poly.addPoint(halfWidth, height);

  public int getIconHeight() {
    return height;

  public int getIconWidth() {
    return width;

  public void paintIcon(Component c, Graphics g, int x, int y) {
    g.translate(x, y);
    if (selected) {
    } else {
    g.translate(-x, -y);


Related examples in the same category

1.Creating a JSlider Component
2.Create a horizontal slider with custom min, max, and value
3.Create a vertical slider with min=0, max=100, value=50
4.Create a vertical slider with custom min, max, and value
5.Make the horizontal slider move right-to-left
6.Make it vertical and move bottom-to-top
7.Make it vertical and move top-to-bottom
8.Get the extent
9.Getting and Setting the Values of a JSlider Component
10.Drawing with Swing, using a JSliderDrawing with Swing, using a JSlider
11.Using progress bars and slidersUsing progress bars and sliders
12.A slider with tick marks and labels
13.An example of JSlider with default labelsAn example of JSlider with default labels
14.Set minor tick marks every 5 units
15.Set major tick marks every 25 units
16.Slider SampleSlider Sample
17.JSlider Sample 2JSlider Sample 2
18.Slider ChangeListenerSlider ChangeListener
19.Inverted SlidersInverted Sliders
20.Slider change action listenerSlider change action listener
21.Date sliderDate slider
22.Tick SlidersTick Sliders
23.Scroll SliderScroll Slider
24.Text SliderText Slider
25.Change the minimum value
26.Change the maximum value
27.Set the extent
28.Move the slider by a fixed amount: the extent.
29.Set all the values at once by using the model
30.Set the value; the new value will be forced into the bar's range
31.Listening for Value Changes in a JSlider Component
32.Constraining JSlider Component Values to Tick Marks
33.Determine if currently snapping to tick marks
34.Snap to tick marks
35.Set to a spot between tick marks; the value moves to closest tick mark
36.Determine if currently painting labels
37.Showing Tick Marks on a JSlider Component
38.Show tick marks
39.The slider allows you to use an arbitrary label at any particular major tick mark.
40.Showing Tick-Mark Labels on a JSlider Component
41.Hide the track
42.A frame with many sliders and a text field to show slider valuesA frame with many sliders and a text field to show slider values