The methods to move the focus to the next or to the previous focusable component are Component.transferFocus() and Component.transferFocusBackward().

This example modifies a component so that pressing the space bar or pressing F2 moves the focus to the next focusable component. Pressing shift space or shift F2 moves the focus to the previous focusable component.

    // Bind space and shift space
component.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke("SPACE"), nextFocusAction.getValue(Action.NAME));
component.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke("shift SPACE"), prevFocusAction.getValue(Action.NAME)); // This key binding is required for text components. It hides the
// default typed space key binding in a text component. If you don't
// hide this key binding, typing the space key will insert a space into
// the text component (as well as move the focus).
// See e1003 覆盖一些和JTextComponent绑定的键 for more details.
component.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke(new Character(' '), 0), "unbound"); // Bind F2 and shift F2
component.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke("F2"), nextFocusAction.getValue(Action.NAME));
component.getInputMap(JComponent.WHEN_FOCUSED).put(
KeyStroke.getKeyStroke("shift F2"), prevFocusAction.getValue(Action.NAME)); // Add actions
component.getActionMap().put(nextFocusAction.getValue(Action.NAME), nextFocusAction);
component.getActionMap().put(prevFocusAction.getValue(Action.NAME), prevFocusAction); // The actions
public Action nextFocusAction = new AbstractAction("Move Focus Forwards") {
public void actionPerformed(ActionEvent evt) {
((Component)evt.getSource()).transferFocus();
}
};
public Action prevFocusAction = new AbstractAction("Move Focus Backwards") {
public void actionPerformed(ActionEvent evt) {
((Component)evt.getSource()).transferFocusBackward();
}
};
Related Examples

e612. Moving the Focus to the Next or Previous Focusable Component的更多相关文章

  1. View Focus的处理过程及ViewGroup的mFocused字段分析

    通过上篇的介绍,我们知道在对KeyEvent的处理中有非常重要的一环,那就是KeyEvent在focus view的path上自上而下的分发, 换句话说只有focus的view才有资格参与KeyEve ...

  2. e617. Determining the Opposite Component of a Focus Event

    The opposite component is the other component affected in a focus event. Specifically, in a focus-lo ...

  3. e616. Determining If a Focus Lost Is Temporary or Permanent

    A temporary focus-lost event occurs if the focus moves to another window. It's temporary because the ...

  4. Selenium Webdriver概述(转)

    Selenium Webdriver https://www.yiibai.com/selenium/selenium_overview.html# webdriver自动化俗称Selenium 2. ...

  5. roundabout旋转幻灯

    jquery.roundabout.js文件/** * jQuery Roundabout - v2.4.2 * http://fredhq.com/projects/roundabout * * M ...

  6. 阻尼滑动--能够滑动过度的ScrollView(OverScrollView)

    贴上一个我自己用过的阻尼滑动的ScrollView,像QQ里面那种滑动效果,尽管不是我写的,可是我认为还能够,贴出来做个记录,实用到的时候免得到处去找. 代码例如以下: /* * Copyright ...

  7. Android 继承framelayout,实现ScrollView 和 HorizontalScrollView 的效果

    有些项目,需要让控件或者布局进行水平和垂直同时能拖拽,当然,ScrollView 和 HorizontalScrollView 的结合写法是一种写法.但是,这么写用户体验效果不佳,会有迟钝感,因此推荐 ...

  8. Android--ScrollView边界回弹效果

    /* * Copyright (C) 2006 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  9. Selenium浏览器自动化测试使用(2)

    Selenium - 环境安装设置 为了开发Selenium RC或webdriver脚本,用户必须确保他们有初始配置完成.有很多关联建立环境的步骤.这里将通过详细的讲解. 下载并安装Java 下载并 ...

随机推荐

  1. Nginx 权限问题

    At my job we are moving to Nginx for the load balancing of our sites. Nginx is a very powerful load ...

  2. .NET Core2.0 环境下MVC模式的支付宝扫码支付接口-沙箱环境开发测试

    所有配置以及相关信息均可以从PC支付中获取 使用的生成二维码的组件名为QRCoder,该组件引用了一个第三方实现的System.Drawing类库,和支付宝官网类似 当面付SDK为Alipay.Aop ...

  3. [Windows Azure] What is a Storage Account?

    What is a Storage Account? A storage account gives your applications access to Windows Azure Blob, T ...

  4. Python Pycharm连接Ubantu Python环境

    由于我习惯在window下开发,但是代码环境布局在Ubantu.使用Python,为了方便程序的调试,尝试在Windows下的Pycharm远程连接到Ubantu虚拟机下的Python环境. 1.准备 ...

  5. Recommender Systems中Yehuda Koren 和 Ma Hao的paper

    以前读了Yehuda Koren和Ma Hao的论文,感觉非常不错,这里分享一下.如果想着具体了解他们近期发的论文,可以去DBLP去看看. Yehuda Koren也是Netflix Prize的冠军 ...

  6. linux 命令(43):bash 快捷键操作

    ctrl 命令行快捷键: CTRL+A: – 快速移动到行首. 我们假设你输入了像下面这样的命令.当你在第 N 行时,你发现在行首字符有一个输入错误 $ gind . -mtime -1 -type ...

  7. iOS开发transform的使用

    // //  ViewController.m //  18-transform的使用 #import "ViewController.h" @interface ViewCont ...

  8. Linux下字符集的安装

    目前环境中经常会遇到编码转化的问题,UTF-8跟GB2312也有问题.只得在Linux上安装GB2312(在Linux操作系统上又称zh_CN.GB2312)的字符集,具体请看下文. Linux下几个 ...

  9. 再论FreeRTOS中的configTOTAL_HEAP_SIZE

    关于任务栈和系统栈的基础知识,可以参考之前的随笔.(点击这里) 这里再次说明:#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 17 * 1024 ) ) 这个 ...

  10. BeautifulSoup 使用select方法详解(通过标签名,类名, id,组合,属性查找)

    import requestsfrom bs4 import BeautifulSoup blslib="html5lib"user_agent="Mozilla/5.0 ...