2

I have a JLayeredPane with 2 layers

the first layer is a JPanel Wrapping an Image. the second layer is another object which extends JPanel called ResizableRectangle and implements KeyListener.

I've overrode the KeyPressed method but it doesn't receive the keyPressed event and the method doesn't get invoked.

I've set the setFocusEnable(true) and used grabFocus(), requestFocus() and requestFocusInWindows() but all of them return false.

I figured out when I press tab after the the JFrame loads , the Focus goes to the panel that I want , and the listener gets the events.

I've added a KeyListener to the JLayeredPane and it works fine but the problem is that i want to add listener to the panel not the layeredPane.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Bamdad Dashtban
  • 354
  • 3
  • 17

1 Answers1

2

I hope that you put Image or ImageIcon to the JLabel then add to the JLabel MouseListener

then just to call

EventQueue.invokeLater(new Runnable() {

     @Override
     public void run() {
        myPanel.grabFocus();
        myPanel.requestFocus();//or requestFocusInWindows();          
     }
});
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I dont want to declare my Resizable Rectangle as a final object , because I want to change it ( Resizable :D ) .Therefore I can't use it in anonymous or inner classes such as your code in runnable. – Bamdad Dashtban Jul 25 '11 at 11:25
  • @Bamdad Dashtban there nothing indicated that your issue(s) corresponding with my post, or my magic globe is out of battery, for sooner/better hepl you have to edit your post with short runnable code that shows ... – mKorbel Jul 25 '11 at 11:31