Objectmix
Tags Register Mark Forums Read

JSlider Set knob Color : Java

This is a discussion on JSlider Set knob Color within the Java forums in Programming Languages category; Hi, I would like to change the JSlider's knob color. I saw an example on the web that used the BasicSliderUI to do that, but it doesn't work well. When I use that it paints correctly the Slider but the rest of the window components are not painted. You can see the code I am using below: slider= new JSlider(JSlider.VERTICAL); slider.setUI(new coloredThumbSliderUI(slider,new Color(0x1b,0x70,0xe1))); public class coloredThumbSliderUI extends BasicSliderUI{ Color thumbColor; coloredThumbSliderUI(JSlider s, Color tColor) { super(s); thumbColor=tColor; } public void paint( Graphics g, JComponent c ) { recalculateIfInsetsChanged(); recalculateIfOrientationChanged(); Rectangle clip = g.getClipBounds(); if ( slider.getPaintTrack() && clip.intersects( trackRect ) ...


Object Mix > Programming Languages > Java > JSlider Set knob Color

Reply

 

LinkBack Thread Tools
  #1  
Old 11-23-2006, 11:22 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default JSlider Set knob Color

Hi,

I would like to change the JSlider's knob color. I saw an example
on the web that used the BasicSliderUI to do that, but it doesn't work
well. When I use that it paints correctly the Slider but the rest of
the window components are not painted.
You can see the code I am using below:



slider= new JSlider(JSlider.VERTICAL);
slider.setUI(new coloredThumbSliderUI(slider,new
Color(0x1b,0x70,0xe1)));

public class coloredThumbSliderUI extends BasicSliderUI{

Color thumbColor;
coloredThumbSliderUI(JSlider s, Color tColor) {
super(s);
thumbColor=tColor;
}

public void paint( Graphics g, JComponent c ) {
recalculateIfInsetsChanged();
recalculateIfOrientationChanged();
Rectangle clip = g.getClipBounds();

if ( slider.getPaintTrack() && clip.intersects( trackRect ) )
{
paintTrack( g );
}
if ( slider.getPaintTicks() && clip.intersects( tickRect ) ) {
paintTicks( g );
}
if ( slider.getPaintLabels() && clip.intersects( labelRect ) )
{
paintLabels( g );
}
if ( slider.hasFocus() && clip.intersects( focusRect ) ) {
paintFocus( g );
}
if ( clip.intersects( thumbRect ) ) {
Color savedColor = slider.getBackground();
slider.setBackground(thumbColor);
paintThumb( g );
slider.setBackground(savedColor);
}
}
}

Please if anyone has an idea of what is the problem please let me
know...I am using java 1.5_07

Thanks in advance,

Carlos Leitao

Reply With Quote
  #2  
Old 11-24-2006, 06:47 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: JSlider Set knob Color


Leitão wrote:
> Hi,
>
> I would like to change the JSlider's knob color. I saw an example
> on the web that used the BasicSliderUI to do that, but it doesn't work
> well. When I use that it paints correctly the Slider but the rest of
> the window components are not painted.
> You can see the code I am using below:
>
> <snip>
>
> Please if anyone has an idea of what is the problem please let me
> know...I am using java 1.5_07
>
> Thanks in advance,
>
> Carlos Leitao


Hi,

I do not get the problem you've got. All components seem to paint
correctly, so the problem you've got could be in another area of your
code.

Besides that, it's possible to change the color of the knob by simply
overriding some UIDefaults values. You could do the following:

java.util.List sliderGradient = Arrays.asList(new Object[] {
new Float(.3f),
new Float(.3f),
new ColorUIResource(0xC8DDF2),
new Color(0x1b, 0x70, 0xe1),
new ColorUIResource( 0x2b, 0x80, 0xF1 )
});

UIManager.getDefaults().put("Slider.gradient", sliderGradient);
UIManager.getDefaults().put("Slider.focusGradient" ,
sliderGradient);

This way you don't have to implement your own UI.

Note that this will work for the MetalLookAndFeel. Other L&F's might
require other settings to change it.

Regards,

Bart

Reply With Quote
  #3  
Old 11-27-2006, 07:23 PM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: JSlider Set knob Color

Bart...Thanks for your answer...it really worked for me. Hey...Where
did you find this properties like "Slider.gradient" and
"Slider.focusGradient". Do you know some place where I can get all the
properties I can set like this.

Thanks again,

Carlos

Bart Cremers escreveu:

> Leitão wrote:
> > Hi,
> >
> > I would like to change the JSlider's knob color. I saw an example
> > on the web that used the BasicSliderUI to do that, but it doesn't work
> > well. When I use that it paints correctly the Slider but the rest of
> > the window components are not painted.
> > You can see the code I am using below:
> >
> > <snip>
> >
> > Please if anyone has an idea of what is the problem please let me
> > know...I am using java 1.5_07
> >
> > Thanks in advance,
> >
> > Carlos Leitao

>
> Hi,
>
> I do not get the problem you've got. All components seem to paint
> correctly, so the problem you've got could be in another area of your
> code.
>
> Besides that, it's possible to change the color of the knob by simply
> overriding some UIDefaults values. You could do the following:
>
> java.util.List sliderGradient = Arrays.asList(new Object[] {
> new Float(.3f),
> new Float(.3f),
> new ColorUIResource(0xC8DDF2),
> new Color(0x1b, 0x70, 0xe1),
> new ColorUIResource( 0x2b, 0x80, 0xF1 )
> });
>
> UIManager.getDefaults().put("Slider.gradient", sliderGradient);
> UIManager.getDefaults().put("Slider.focusGradient" ,
> sliderGradient);
>
> This way you don't have to implement your own UI.
>
> Note that this will work for the MetalLookAndFeel. Other L&F's might
> require other settings to change it.
>
> Regards,
>
> Bart


Reply With Quote
  #4  
Old 11-28-2006, 01:45 AM
Junior Member
 
Join Date: Nov 2009
Posts: 0
Application Development is on a distinguished road
Default Re: JSlider Set knob Color

Leitão schrieb:
> Bart...Thanks for your answer...it really worked for me. Hey...Where
> did you find this properties like "Slider.gradient" and
> "Slider.focusGradient". Do you know some place where I can get all the
> properties I can set like this.
>


It's always good to have a look at the source code. This way you learn
much more than just the available properties. Of course, this is only
possible if you have the source code of the look & feel under
investigation...

Anyway, to get a list of properties of the current look & feel:

System.out.println( UIManager.getLookAndFeelDefaults().keySet() );

Or to get an alphabetically ordered list:

List<Object> keys = new ArrayList<Object>(
UIManager.getLookAndFeelDefaults().keySet() );

Collections.sort( keys, new Comparator<Object>() {
public int compare( Object o1, Object o2) {
return ((String)o1).compareTo((String)o2);
}
public boolean equals() { return false; }
});

for ( Object key : keys )
System.out.println( key );


Bye
Michael
Reply With Quote
Reply

Thread Tools


Similar Threads

Thread Thread Starter Forum Replies Last Post
Re: Logarithmic Knob Increments usenet labview 0 09-26-2007 06:10 PM
Re: Knob usenet labview 0 08-07-2007 11:40 PM
Re: Knob Control with hex values for the scale usenet labview 0 08-02-2007 02:10 PM
Update GUI while JSlider is moving usenet Java 4 06-11-2007 03:14 PM
moving on to jslider... usenet Java 1 04-29-2007 07:11 PM


All times are GMT -5. The time now is 07:03 AM.