**********************************************************************
COMPLETE SOURCE CODE FOR : MyNewMetalSliderUI.java
**********************************************************************
import javax.swing.*;
import java.awt.*;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.metal.MetalSliderUI;
import javax.swing.plaf.ComponentUI;
public class MyNewMetalSliderUI extends MetalSliderUI
{
// Create our own slider UI
public static ComponentUI createUI(JComponent c )
{
return new MyNewMetalSliderUI();
}
//*******************HORIZONTAL MAJOR TICK*******************
public void paintMajorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;
if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=255,GREEN=0,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(255,0,0);
//Set color that will use to draw MAJOR TICK using created color
g.setColor(majorTickColor);
//Draw MAJOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height);
}
}
//*******************HORIZONTAL MAJOR TICK*******************
//*******************HORIZONTAL MINOR TICK*******************
public void paintMinorTickForHorizSlider(Graphics g, Rectangle tickBounds, int x)
{
int coordinateX=x;
if(slider.getOrientation()==JSlider.HORIZONTAL)
{
//Create color using RGB value(RED=12,GREEN=255,BLUE=0)
//You can use Color picker above to get RGB value for color that you want
Color majorTickColor=new Color(12,255,0);
//Set color that will use to draw MINOR TICK using created color
g.setColor(majorTickColor);
//Draw MINOR TICK
g.drawLine(coordinateX,0,coordinateX,tickBounds.height/2);
}
}
//*******************HORIZONTAL MINOR TICK*******************
}
**********************************************************************
JUST COMPILE
**********************************************************************
**********************************************************************
COMPLETE SOURCE CODE FOR : SetJSliderTickColor.java
**********************************************************************
import javax.swing.*;
import java.awt.*;
public class SetJSliderTickColor
{
public SetJSliderTickColor()
{
//Create slider using JSlider
JSlider mySlider=new JSlider();
//Set major tick spacing to 10
mySlider.setMajorTickSpacing(10);
//Set minor tick spacing to 1
mySlider.setMinorTickSpacing(1);
//Make slider's numbers visible
mySlider.setPaintLabels(true);
//Make slider's ticks visible
mySlider.setPaintTicks(true);
//Create a window using JFrame with title ( Set JSlider tick color )
JFrame frame=new JFrame("Set JSlider tick color");
//Set JFrame layout to FlowLayout
frame.setLayout(new FlowLayout());
//Add created JSlider into JFrame
frame.add(mySlider);
//Set JFrame's default close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set JFrame size
frame.setSize(500,300);
//Set JFrame locate at center of screen
frame.setLocationRelativeTo(null);
//Make JFrame visible
frame.setVisible(true);
}
public static void main(String[]args)
{
/***********Set our own UI Delegate for Slider*************/
UIManager uim=new UIManager();
//MyNewMetalSliderUI contain settings that use to change tick color
uim.put("SliderUI","MyNewMetalSliderUI");
/***********Set our own UI Delegate for Slider*************/
SetJSliderTickColor sjstc=new SetJSliderTickColor();
}
}
**********************************************************************
JUST COMPILE AND EXECUTE
**********************************************************************