Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

Gradient background on a JSlider

Can I put a gradient background on a JSlider without creating a subClassed JSlider, and if not, how would I need to modify the JSlider to give it a gradient background (Also, both with a background file, and if possible, by just specifying the two end colors?)
0
riaancornelius
Asked:
riaancornelius
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
objectsCommented:
you'd write a custom ui for your slider

http://blog.elevenworks.com/?p=4
0
 
riaancorneliusAuthor Commented:
awesome tutorial,
and that will definately be handy, but for this specific problem, I need to change only one JSlider and leave all others unchanged...

Thinking about it, It would be quite simple to create a new component and just extend paintComponent, but is there a way to add it onto an existing component without creating a new component?
0
 
objectsCommented:
> I need to change only one JSlider and leave all others unchanged...

u can use different ui for different instances of the same component.

> but is there a way to add it onto an existing component without creating a new component?

yes, by using a custom ui ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ksivananthCommented:
how about developing custom border?
0
 
riaancorneliusAuthor Commented:
>> u can use different ui for different instances of the same component.
That makes sense. Will try that. Thanks Objects.

ksivananth, I need something less subtle than that.
0
 
stimpyjcatCommented:
Try this AbstractComponentDecorator, which will let you decorate the background of one component at a time:

http://rabbit-hole.blogspot.com/2006/04/decoratingoverpainting-swing.html

Of course, it depends on how much background the original UI leaves to be painted.

Or, you could just wrap it in a JPanel and make the JPanel paint the gradient background.
0
 
ksivananthCommented:
to set gradient background to panel,

    class MyPanel extends JPanel{

        public void paintComponent( Graphics g ){
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint( new GradientPaint((float)   this.getX(),(float) this.getY(), Color.BLUE, (float) this.getX(), (float) this.getY()+ this.getHeight() - 2, Color.WHITE, true));
            g2.fillRoundRect(0,0, this.getWidth(),this.getHeight(), 12, 12);
        }
    }

I feel this is a easiest one!
0
 
riaancorneliusAuthor Commented:
>> I feel this is a easiest one!
I agree, so I'm using it, but I think objects way is the right way to do it, and I know I'll be using that code in future, so I'm splitting points.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now