开发者

Grab focus to a JPanel in a JLayeredPane

开发者 https://www.devze.com 2023-03-22 11:33 出处:网络
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 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 开发者_Go百科the problem is that i want to add listener to the panel not the layeredPane.


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();          
     }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号