Title: color java JSlider based on tick value
Tags: java;swing;colors;jslider
Question: I want to have a JSlider with three kind of colors each one occupying a range of values(eg. 1 to 10 is green, 10 to 20 is yellow, 20 to 30 is red), how can this be implemented?

Comment: Do you want the background to have three stripes of color? Or do you want it to be colored one of the three colors based on the selected value?

Comment: @Russell Zahniser: I want the background to have three stripes of color, how can I make GradientPaint to do that to the background JPanel

Here is the accepted answer: Edit:

Oops, for some reason I thought there was a ```paintBackground()``` method in JComponent. I guess you'd instead have to do ```setOpaque(false)``` (so that ```super``` doesn't paint the background) and then override ```paintComponent()``` like this:

```protected void paintComponent(Graphics g) {
   int w = getWidth();
   int h = getHeight();
   int x1 = w / 3;
   int x2 = w * 2 / 3;

   g.setColor(Color.GREEN);
   g.fillRect(0, 0, x1, h)
   g.setColor(Color.YELLOW);
   g.fillRect(x1, 0, x2 - x1, h)
   g.setColor(Color.RED);
   g.fillRect(x2, 0, w - x2, h)

   super.paintComponent();
}
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:04/30/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><swing><colors><jslider>', 'Title': 'color java JSlider based on tick value', 'ViewCount': 760, 'id': 10390251, 'user_AboutMe': None, 'user_AccountId': 41672.0, 'user_DisplayName': 'user121196', 'user_Reputation': 29162.0, 'user_Views': 1471.0, 'user_id': 121196.0}}