Label JSlider Positions: use text to mark JSlider in Java

Description

The following code shows how to label JSlider Positions: use text to mark JSlider.

Example


 /*from  ww w .  j  ava 2s. com*/
import java.awt.BorderLayout;
import java.util.Hashtable;

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

public class Main {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JSlider jSliderOne = new JSlider();

    jSliderOne.setPaintLabels(true);
    
    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
    table.put (0, new JLabel(new ImageIcon("yourFile.gif")));
    table.put (10, new JLabel("Ten"));
    table.put (25, new JLabel("Twenty-Five"));
    table.put (34, new JLabel("Thirty-Four"));
    table.put (52, new JLabel("Fifty-Two"));
    table.put (70, new JLabel("Seventy"));
    table.put (82, new JLabel("Eighty-Two"));
    jSliderOne.setLabelTable (table);
    
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

The code above generates the following result.

Label JSlider Positions: use text to mark JSlider in Java




















Home »
  Java Tutorial »
    Swing »




Action
Border
Color Chooser
Drag and Drop
Event
Font Chooser
JButton
JCheckBox
JComboBox
JDialog
JEditorPane
JFileChooser
JFormattedText
JFrame
JLabel
JList
JOptionPane
JPasswordField
JProgressBar
JRadioButton
JScrollBar
JScrollPane
JSeparator
JSlider
JSpinner
JSplitPane
JTabbedPane
JTable
JTextArea
JTextField
JTextPane
JToggleButton
JToolTip
JTree
Layout
Menu
Timer