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. .NET CORE EF 框架调用存储过程

    ; //多个参数多表组合值 SqlParameter[] Param = { new SqlParameter("@UserId", System.Data.SqlDbType.V ...

  2. 如何评测一个P2P平台是否可靠

    1.标准 1)平台是否实现银行存管 2)平台是否有ICP许可证 ICP许可证:也称互联网信息服务业务经营许可证,或者增值电信业务许可证中的互联网信息服务业务. 3)平台背景 经营主体背景(央企,国企, ...

  3. 每日英语:Patent Wars Erupt Again in Tech Sector

    The long-running patent war among the technology industry's heavyweights just grew a whole lot bigge ...

  4. Docker 入门(Mac环境)- part 3 服务(services)

    part-3 服务(services) 简介 一个应用的规模的扩大是很常见的事情,会经常用到负载均衡这些,如要实现这些功能,我们就会用到docker中更高一层的东西-service(服务). 比如说一 ...

  5. C# FileStream 文件复制

    方法: /// <summary> /// 文件复制 /// </summary> /// <param name="source">源文件,例 ...

  6. LeetCode: Best Time to Buy and Sell Stock 解题报告

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  7. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

    两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...

  8. asp.net基于StateServer的二级域名共享session

    备注:亲自试验有效,如果网友有通过下面的教程未实现session共享的,欢迎留言说明你遇到的问题.必有回复. 最近为实现的二级域名共享session纠结好久.网上的很多实现的方法试了都不行,查了很久才 ...

  9. Charles做代理的Map Remote路径配置

    使用Webpack做前端开发时,本地localhost使用了Webpack-dev-server搭建一个服务,开发阶段如果想把对本地后台的请求改为对外网的请求,可以使用Charles开启代理,把路径修 ...

  10. ActiveMQ实战篇之ActiveMQ实现request/reply模型(二)

    ActiveMQ实战篇之ActiveMQ实现request/reply模型(二)