Set JSlider tick color

Complete source code below will show you, how to set JSlider tick color. I have tried to find method in JSlider class to set JSlider tick color...but, i can't find it. So i tried to create UI Delegate only for SliderUI that change JSlider tick color. You can try to compile both source code below. Make sure both java file below locate at the same location. JSlider's tick color is set in MyNewMetalSliderUI.java.You can try to change it's color follow what color that you want base on RGB value. After you compile SetJSliderTickColor.java and MyNewMetalSliderUI.java, you try execute SetJSliderTickColor.java. This is because, main method contain in this file.Oooo...before i forget, it's only handle horizontal slider. If you want to make it can use for vertical slider, you can try overwrite method paintMajorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) and paintMinorTickForVertSlider(Graphics g, Rectangle tickBounds, int y) in MetalSliderUI class. You can review it in Java SE API for MetalSliderUI class.

**********************************************************************
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
**********************************************************************
Related Posts Plugin for WordPress, Blogger...

I Also Can Create T-Shirt Logo For You