码迷,mamicode.com
首页 > 其他好文 > 详细

e621. Activating a Keystroke When Any Child Component Has Focus

时间:2018-09-06 11:06:05      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:tail   body   cells   tor   tac   cond   str   example   available   

Normally, a keystroke registered on a component is activated when the component has the focus. This type of activation condition is called WHEN_FOCUSED. It is possible to specify that a keystroke be activated if it or any child or descendant component has the focus. This type of keystroke activation condition is called WHEN_ANCESTOR_OF_FOCUSED_COMPONENT.

An example where this type of keystroke activation condition is useful is in the case of a scrollpane that handles scrolling navigation keystrokes even when the child component has the focus.

There are three types of activation conditions available: WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, and WHEN_IN_FOCUSED_WINDOW. See e620 Activating a Keystroke When Any Component in the Window Has Focus for more details about these activation conditions.

    // To create an action, see e855 创建一个事件
    
    // Register keystroke
    component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
        KeyStroke.getKeyStroke("F2"), action.getValue(Action.NAME));
    
    // Register action
    component.getActionMap().put(action.getValue(Action.NAME), action);

 

Related Examples

e621. Activating a Keystroke When Any Child Component Has Focus

标签:tail   body   cells   tor   tac   cond   str   example   available   

原文地址:https://www.cnblogs.com/borter/p/9596082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!