Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent JPanel in JLayeredPane

I've seen several similar questions, but unfortunately the suggestions contained therein haven't quite done it for me.

The overall goal: I have a slider that I want to display in the upper left of a window that mainly displays a visualization (here "display"). The slider should overlay on top of the visualization without any background.

So far, I have a JPanel containing my slider on a layer above the visualization in a JLayeredPane. The problem is that, despite calls to setOpaque and/or setBackground(new Color(0,0,0,0)), the background for the JPanel including the slider appears grey. I am using the Nimbus LAF.

Originally, I used an Absolute Layout, which accomplished almost everything quite elegantly and with minimal code. The issue there was that the visualization didn't resize with the window automatically (but I'm open to this approach if there's a clean way to make the visualization resize with the window).

JPanel sliderPane = new JPanel();
sliderPane.setLayout(new BoxLayout(sliderPane, BoxLayout.X_AXIS));
sliderPane.add(zoomSlider);
zoomSlider.setMaximumSize(new Dimension (60, 150));
sliderPane.add(Box.createHorizontalGlue());
sliderPane.setBackground(new Color(0,0,0,0));

JLayeredPane graphPane = new JLayeredPane();
graphPane.setPreferredSize(new Dimension(1000, 800));
graphPane.setLayout(new BoxLayout(graphPane, BoxLayout.Y_AXIS));
graphPane.add(sliderPane, new Integer(1));
graphPane.add(display, new Integer(0));
graphPane.setOpaque(false);

Thanks so much!

like image 947
bcr Avatar asked Mar 08 '26 12:03

bcr


1 Answers

Rendering the slider is the responsibility of the component's UI delegate, typically a subclass of BasicSliderUI. If required, you can substitute your own implementation, as shown here.

image

like image 154
trashgod Avatar answered Mar 11 '26 00:03

trashgod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!