2

I'm trying to create a simple piano application using Java, and I'm stuck at creating the keys.

Awesome

So what I have is a bunch of (Black) JButtons on top of (White) JButtons, but whenever I click the white buttons, it covers the black buttons.

So my question is; how do I implement this so the black JButtons would stay on top of the white ones.

Kevin D.
  • 1,500
  • 1
  • 18
  • 41
  • How are you positioning the `JButton`s in the first place? Possibly, JLayeredPane is the way to go for you. – Aleks G Jan 11 '12 at 15:32
  • *"how do I implement this so the black JButtons would stay on top of the white ones."* What is your current code? Can you post an [SSCCE](http://sscce.org/)? – Andrew Thompson Jan 11 '12 at 15:37

1 Answers1

2

I'm not sure how you're laying out your buttons in the first place. I suggest you use JLayeredPane for the layout. With it, you specify z-index of each component - and it takes care of the rest. Have a look here: http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html#depth

Aleks G
  • 56,435
  • 29
  • 168
  • 265