开发者

ActionS in Swing ContainerS (JFrameS etc)

开发者 https://www.devze.com 2023-01-28 01:27 出处:网络
It seems that while you can add ActionS to ComponentS, you can\'t add them to ContainerS such as JFrame, JApplet or JDi开发者_JAVA技巧alog. Since I could find nothing helpful on Google, I felt I shoul

It seems that while you can add ActionS to ComponentS, you can't add them to ContainerS such as JFrame, JApplet or JDi开发者_JAVA技巧alog. Since I could find nothing helpful on Google, I felt I should share my findings so people in the future don't have to waste too much time. Please feel free to add information if you think I missed something or let something out.

The solution lies in .getRootPane(), which will then allow you to add actions like so:

jframe.getRootPane().getInputMap(JComponent.WHEN_FOCUSED).(KeyStroke.getKeyStroke("INSERT"), insertAction);

Hope this will be helpful to future generations.


Sure you can add Key Bindings to a JPanel. Its a Swing component.

I'm not sure that the "WHEN_FOCUSED" InputMap will work. Normally focus is on a component, not the root pane.

Check out Key Bindings for a list of the default bindings and my simple example of using Key Bindings.

0

精彩评论

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