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?
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.
posted 15 years ago
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.
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?
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.
posted 15 years ago
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!
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.