• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSlider knob color

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've scoured the net for the answer and come up short. How do I change the knob color on the JSlider?

Also, could you give your advice if the JSlider is the correct component. What this part of my program does is tell the user how much space he still left of his allocated 5 MB. Would another component be better?

Thanks!
Rachel
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll look for the color issue in a moment. But I wanted to let you know I don't think a JSlider is the way to go. A JSlider typically allows the user to adjust some property. So if the memory is not meant to be adjusted there are a few components that would work better.

  • Progress Bar - This is the one I would use. It gives you somewhat of a graphical representation of your memory usage and allows a number to be displayed.
  • You could simply use a JLabel
  • You could also just use a JTextField that is not editable.


  • Like I said, I would do the Progress Bar. You'll see why when you look at the examples in the link I sent you. If you still want to use a JSlider, let me know and I will see what I can find out about the color of the knob.
     
    Rachel Swailes
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I've been thinking about it, and I'm still keen on JSlider.

    I know that I don't want the user to change the knob but I need the amount displayed in ticks like the JSlider along the side.
    The JProgressbar would be more perfect as I could color the whole thing in, but I need the amount displayed in value and not in percentage.

    So I think that I will be sticking to the JSlider for now and if I have time to play with the JProgress Bar to put ticks on it, I'll try nearer the end of the project.

    So any clues to the knob color?

    Thanks!
    rachel
     
    Gregg Bolinger
    Ranch Hand
    Posts: 15304
    6
    Mac OS X IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This code should get you started. The knob is rectangle in this example but you should get the idea.

     
    Rachel Swailes
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks!!
     
    Rachel Swailes
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't mean to sound silly, but what exactly does this do? Why do we have to setUI()?

    Also, in the paint method, what is trackRect?

    Cheers,
    Rachel
     
    Gregg Bolinger
    Ranch Hand
    Posts: 15304
    6
    Mac OS X IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Rachel Swailes:
    I don't mean to sound silly, but what exactly does this do? Why do we have to setUI()?

    Also, in the paint method, what is trackRect?

    Cheers,
    Rachel



    You have to setUI because you are basically overriding the BasicSliderIU class so that you can do what you want (make the knob a different color). Sadly, there is not setKnobColor type method and this is the only way to go about it. Pain in the ass, huh.

    trackRect is a protected variable of the BasicSliderUI class. It's basically a Rectangle that represents the bounds of the slider's track. Take a look at the API for BasicSliderUI. It will clear up some of the confusion. But not all. I still am far from a whiz at custom UI's.
     
    Rachel Swailes
    Ranch Hand
    Posts: 434
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Greg!

    I got stuck there for a moment, then I realised I'd miss placed a bracket! Silly me! Pity about no "setKnobColor()", oh well, more of the challenge I guess!

    Thanks again
    Rachel
     
    Ranch Hand
    Posts: 299
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you just have one slider, you can also try using the UIManager to set the default colors. There are 5 that deal with sliders:
    Slider.background
    Slider.focus
    Slider.foreground
    Slider.highlight
    Slider.shadow

    You can use them like so:
    UIManager.put("Slider.foreground", Color.red);
    UIManager.put("Slider.focus", Color.red);
    UIManager.put("Slider.highlight", Color.red);
    UIManager.put("Slider.shadow", Color.red);
    UIManager.put("Slider.background", Color.red);

    This changes a JSlider to MOSTLY red - there is still a rectangle of the metal purple color. Must be some other UIManger setting to get that to change colors. I didn't look around for it.

    Brian
     
    Gregg Bolinger
    Ranch Hand
    Posts: 15304
    6
    Mac OS X IntelliJ IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I tried a lot of those. Didn't really see that much of a change though in the color.

    I have been trying to find a resource that has the UIManager options or Client Properties for every Swing component, but I can't seem to find a resource like that. Do you know of one online?
    reply
      Bookmark Topic Watch Topic
    • New Topic