4

I have an applet. I set its image background. It works fine.
Now I want to set a background image to a JSlider.
How can I do that?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117

1 Answers1

0

You will need to create a custom JSlider class and override the paintComponent method. Be sure to call setOpaque(false) on your slider object.

public class CustomSlider extends JSlider
{
    private Image img = null;

    public CustomSlider()
    {
        try
        {
            img = ImageIO.read(new File("background.jpg"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        // Draw the previously loaded image to Component
        g.drawImage(img, 0, 0, null);
        super.paintComponent(g);
    }
}