Short question about JSlider  
Author Message
aurengo





PostPosted: 2006-8-25 4:15:00 Top

java-programmer, Short question about JSlider Hi,

I have a problem with the font of JSlider labels.
This code compiles but does not work and I am wondering how I can set
the label font :

JSlider s = new JSlider(JSlider.HORIZONTAL,min,max,value);
Font f = new Font("Monaco", Font.PLAIN,9);
s.setFont(f);
...

Thanks for your help and suggestions

 
Ben Kraufmann





PostPosted: 2006-8-25 4:59:00 Top

java-programmer >> Short question about JSlider On Thu, 24 Aug 2006 22:15:08 +0200, aurengo wrote:

> Hi,
>
> I have a problem with the font of JSlider labels.
> This code compiles but does not work and I am wondering how I can set
> the label font :
>
> JSlider s = new JSlider(JSlider.HORIZONTAL,min,max,value);
> Font f = new Font("Monaco", Font.PLAIN,9);
> s.setFont(f);
> ...
>
> Thanks for your help and suggestions


Hi,

it's a known bug that JSlider ignores setFont().
So, extending JSlider with your own labels may be a good idea.
Or getting in touch with Java6? ;)

Ben
 
Michael Rauscher





PostPosted: 2006-8-25 5:17:00 Top

java-programmer >> Short question about JSlider Ben Kraufmann schrieb:
> On Thu, 24 Aug 2006 22:15:08 +0200, aurengo wrote:
>
>> Hi,
>>
>> I have a problem with the font of JSlider labels.
>> This code compiles but does not work and I am wondering how I can set
>> the label font :
>>
>> JSlider s = new JSlider(JSlider.HORIZONTAL,min,max,value);
>> Font f = new Font("Monaco", Font.PLAIN,9);
>> s.setFont(f);
>> ...
>>
>> Thanks for your help and suggestions
>
>
> Hi,
>
> it's a known bug that JSlider ignores setFont().

It's not a bug, it's a feature (although I've to admit that it would
have been nice if the slider's font had been taken into account):

http://java.sun.com/docs/books/tutorial/uiswing/components/slider.html

> So, extending JSlider with your own labels may be a good idea.

That is a bad idea. The OP simply needs to do what the documentation
tells him to do:

Font font = slider.getFont();
Dictionary dict = slider.getLabelTable();
for ( Enumeration e = dict.elements(); e.hasMoreElements(); ) {
Object element = e.nextElement();
if ( element instanceof JComponent ) {
((JComponent)element).setFont( font );
}
}

Bye
Michael