Changing the color of a JSlider component

The JSlider component consists of several subcomponents (ticks, lines, labels, knob, …). Look at the plaf UI source code (eg. javax.swing.plaf.basic.BasicSliderUI) and you’ll notice that several times Color.black is hardcoded. So unless you rewrite a custom UI class for your JSlider, you will only be able to partially control the overall color.

This following example shows you how to set the JSlider fore and background color as well as the fore and background color for the labels.

Main.java:

import javax.swing.plaf.metal.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
      // draw the major tick marks (one for every tick label)
      slider.setMajorTickSpacing(10);
      // draw the minor tick marks (between the tick labels)
      slider.setMinorTickSpacing(1);
      // draw the tick marks
      slider.setPaintTicks(true);
      // draw the tick mark labels
      slider.setPaintLabels(true);
 
      slider.setForeground(Color.red);
      slider.setBackground(Color.yellow);
 
      Dictionary dictionary = slider.getLabelTable();
      if (dictionary != null) {
         Enumeration keys = dictionary.keys();
         while (keys.hasMoreElements()) {
            JLabel label = (JLabel) dictionary.get(keys.nextElement());
            label.setForeground(Color.blue);
            // uncomment these following lines to get a background for your labels
            label.setOpaque(true);
            label.setBackground(Color.green); 
         }
      }
 
      getContentPane().add(slider);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}