title: How do I make a JSlider background transparent? or How do I properly overlay JComponents in JLayeredPane?
body: <p>I am trying to overlay a JSlider on top of a JProgressBar. I am using a JLayeredPane to hold the two components. I am adding the JProgressBar to the JLayeredPane and then adding the JSlider. So far, I have attempted to make the JSlider transparent by setting opaque to false and overriding the paintComponent method. What I end up with is that the slider handle is the only part that becomes transparent while the background stays opaque. It is possible that I am not using the JLayeredPane correctly, but my tests with JLabels seemed to work. How do I get the background of the JSlider to be transparent?</p>
  
  <pre><code>JSlider slider = new JSlider()
  {
  
      @Override
      public void paintComponent(java.awt.Graphics g)
      {
          java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
          g2.setComposite(AlphaComposite.getInstance(
                  AlphaComposite.SRC_OVER, (float) 0.5));
  
          super.paintComponent(g2);
      }
  
  };
  </code></pre>
  
  <p>Thank you all for your help. Through your examples, I have discovered that my issue is with the JLayeredPane that I am using. I recognize that the JSlider background can be made transparent, however, I still cannot get the components on layers beneath to show through. This is my example:</p>
  
  <pre><code>public class SliderTest extends JFrame
  {
  
  private JPanel contentPane;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args)
  {
      EventQueue.invokeLater(new Runnable()
      {
          public void run()
          {
              try
              {
                  SliderTest frame = new SliderTest();
                  frame.setVisible(true);
              } catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the frame.
   */
  public SliderTest()
  {
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setBounds(100, 100, 450, 300);
      contentPane = new JPanel();
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      contentPane.setLayout(new BorderLayout(0, 0));
      setContentPane(contentPane);
  
      JPanel panel = new JPanel();
      contentPane.add(panel, BorderLayout.CENTER);
      panel.setLayout(new BorderLayout(0, 0));
  
      JLayeredPane layeredPane = new JLayeredPane();
  
      panel.add(layeredPane, BorderLayout.CENTER);
      layeredPane.setLayout(new BorderLayout(0, 0));
  
      JProgressBar progressBar = new JProgressBar();
      progressBar.setValue(50);
      layeredPane.add(progressBar);
      layeredPane.setLayer(progressBar, 0);
  
      JSlider slider = new JSlider()
      {
          @Override
          protected void paintComponent(Graphics g)
          {
              Graphics2D g2d = (Graphics2D) g.create();
              g2d.setColor(getBackground());
              g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
              g2d.fillRect(0, 0, getWidth(), getHeight());
              g2d.dispose();
  
              super.paintComponent(g);
          }
  
      };
      layeredPane.setLayer(slider, 1);
      slider.setOpaque(false);
  
      // layeredPane.setLayer(slider, 1);
      layeredPane.add(slider, BorderLayout.CENTER);
  
  }
  </code></pre>
  
  <p>}</p>