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. C++技术沙龙报名开始啦!

    沙龙主题:C++甜点关键字:C++之美,黑科技,神奇和魔力内容:三场主题演讲和一场开放性话题讨论时间:2015年5月16日下午2:00-6:00地点:珠海金山办公软件1楼VIP厅,珠海市吉大景山路莲山 ...

  2. SVN Cleanup failed to process the following paths错误的解决

    在使用TortoiseSVN工具执行Cleanup操作时经常出现Cleanup failed to process the following paths的错误,具体如下图: 网上搜索了一下,找到了解 ...

  3. Tornado使用-队列Queue

    1.tornado队列的特点 和python标准队列queue相比,tornado的队列Queue支持异步 2.Queue常用方法 Queue.get() 会暂停,直到queue中有元素 Queue. ...

  4. Implementation Notes: Runtime Environment Map Filtering for Image Based Lighting

    https://placeholderart.wordpress.com/2015/07/28/implementation-notes-runtime-environment-map-filteri ...

  5. ZOOKEEPER解惑[转]

    今年年初的时候,写了一篇ZooKeeper的入门文章<初识ZooKeeper>,一直到这一周,才有时间将ZooKeeper整个源码通读了一遍.不能说完全理解了ZooKeeper的工作原理与 ...

  6. SQL2008R2 安装图解

    安装SQL Server 2008 R2需要.NET Framework 3.5 SP1支持 这里我们的操作系统是Windows Server 2008 R2,已经默认自带了.NET Framewor ...

  7. 查看SQL实际内存占用

    如果打开了AWE,在任务管理器中就看不到实际的内存使用了.可以用SQL语句来查内存占用,或是“性能监视器(Performance Monitor)中的SQLServer: Memory Manager ...

  8. csv和excel互转

    Python csv转换为excel学习笔记: openpyxl模块需要安装pip install openpyxl import openpyxl import csv '''读取csv文件写入ex ...

  9. CSS超过指定的宽度加省略号

    /*table-layout:fixed 会使表格均等分*/ #TreeView1 table { width:290px; table-layout: fixed; } #TreeView1 td: ...

  10. SQL复制表操作

    select * into tb1 from tb2 insert into tb1 (fld1, fld2)  select fld1, 0 from tb2 where fld0='x' 以上两句 ...