3

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!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
bcr
  • 1,328
  • 11
  • 27

1 Answers1

2

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

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    This is a reasonable answer, so I'll go with it. I've uncovered a bit of the reason for my issue and chosen a different approach, however: the issue is that assigning a box layout to a JLayeredPane sets the whole pane to one instance of that layout, not one layout per layer (as I was hoping), so the reason there's a bg on the pane is because there is nothing under there at all. I opted for using the AbsoluteLayout positioning and overriding componentResized to resize the component that needs to resize with the window. – bcr Jul 07 '12 at 17:31
  • Thanks, but don't hesitate to add a complementary answer if it might help future readers. I'm always interested in cross-platform UI pitfalls. – trashgod Jul 07 '12 at 17:38