A Java JSlider configuration example

Here’s a little snippet of code that shows how to create a Java JSlider, and then configure the JSlider. The code is written in Scala, which as you can see, is remarkably similar to Java:

val volumeSlider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 50)

// somewhere later in the code ...
def configureVolumeSliderControl {
  volumeSlider.setToolTipText("Volume control")
  volumeSlider.setMajorTickSpacing(10)
  volumeSlider.setMinorTickSpacing(2)
  volumeSlider.setPaintTicks(true)
  volumeSlider.setPaintLabels(true)
  
  val labelTable = new Hashtable[Integer, JLabel]()
  labelTable.put(new Integer(0), new JLabel("Quiet"))
  labelTable.put(new Integer(100), new JLabel("Loud"))
  volumeSlider.setLabelTable(labelTable)
}

The JSlider is shown in the lower-right of my Blue Parrot Swing application:

If you needed a Java JSlider example, I hope this has been helpful.