Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2006

Tip: Looking for answers? Try searching our database.

JSlider Set knob Color

Thread view: 
Leitão - 23 Nov 2006 16:22 GMT
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
Bart Cremers - 24 Nov 2006 11:46 GMT
Leit?o wrote:
> Hi,
>
[quoted text clipped - 12 lines]
>
>                           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
Leitão - 28 Nov 2006 00:23 GMT
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

> > Hi,
> >
[quoted text clipped - 42 lines]
>
> Bart
Michael Rauscher - 28 Nov 2006 06:41 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.