There is no straightforward way to set the initial focused component in a window. The typical method is to add a window listener to listen for the window opened event and then make the desired component request the focus.

    // Create frame and three buttons
JFrame frame = new JFrame();
JButton component1 = new JButton("1");
JButton component2 = new JButton("2");
JButton component3 = new JButton("3"); // Set component with initial focus; must be done before the frame is made visible
InitialFocusSetter.setInitialFocus(frame, component2); class InitialFocusSetter {
public static void setInitialFocus(Window w, Component c) {
w.addWindowListener(new FocusSetter(c));
} public static class FocusSetter extends WindowAdapter {
Component initComp;
FocusSetter(Component c) {
initComp = c;
}
public void windowOpened(WindowEvent e) {
initComp.requestFocus(); // Since this listener is no longer needed, remove it
e.getWindow().removeWindowListener(this);
}
}
}
Related Examples

e614. Setting the Initial Focused Component in a Window的更多相关文章

  1. e636. Listening to All Key Events Before Delivery to Focused Component

    Registering a key event dispatcher with the keyboard focus manager allows you to see all key events ...

  2. e620. Activating a Keystroke When Any Component in the Window Has Focus

    Normally, a keystroke registered to a component is activated when the component has the focus. This ...

  3. How a non-windowed component can receive messages from Windows -- AllocateHWnd

    http://www.delphidabbler.com/articles?article=1 Why do it? Sometimes we need a non-windowed componen ...

  4. How a non-windowed component can receive messages from Windows

    Why do it? Sometimes we need a non-windowed component (i.e. one that isn't derived fromTWinControl) ...

  5. e621. Activating a Keystroke When Any Child Component Has Focus

    Normally, a keystroke registered on a component is activated when the component has the focus. This ...

  6. cvpr2015papers

    @http://www-cs-faculty.stanford.edu/people/karpathy/cvpr2015papers/ CVPR 2015 papers (in nicer forma ...

  7. 提高神经网络的学习方式Improving the way neural networks learn

    When a golf player is first learning to play golf, they usually spend most of their time developing ...

  8. PA教材提纲 TAW12-2

    Unit1 Adjustment of SAP Standard Software(SAP标准软件修改) 1.1 Adjusting SAP Standard Software(如何修改SAP标准软件 ...

  9. tmux手册中文翻译

    man tmux可以看到最详细的tmux介绍,本文翻译自tmux手册. tmux全名叫"terminal multiplexer",终端多路复用器. tmux的命令格式为: tmu ...

随机推荐

  1. Gartner:2018人工智能技术成熟度曲线

    https://www.secrss.com/articles/4392 人工智能被广为关注,但是一些想法恐难达到预期.本成熟度曲线将追踪AI基本趋势和未来创新,以确定人工智能技术发展的范围.状态.价 ...

  2. linux命令(44):sed,vim;去掉文件中的^M 符号,去掉行首空格和制表符

    注:下面的直接粘贴会出错,要重新输入^M 如何输入^M,先 ctrl+v,然后 ctrl+m 第一种方法: cat -A filename 就可以看到windows下的断元字符 ^M要去除他,最简单用 ...

  3. Mathematica .nb程序运行不下去的原因

    Mathematica是个不错的工具,尤其是其支持交互式参数调整的plot功能,灰常实用.但一直有个烦人的carveat,这里提一下. 在evaluate notebook(.nb)时,一旦碰到了使用 ...

  4. pre 标签的使用注意事项

    .news-msg{ // padding: 5px; white-space: pre-wrap; word-wrap: break-word; font-family:'微软雅黑'; } < ...

  5. 双重检验的单例模式,为什么要用volatile关键字

    双重检验的单例模式是比较推荐的单例写法,在该代码中的单例对象的是用volatile关键字修饰的.这时就产生的一个疑问,为什么需要volatile来修饰呢?上网查看多个博客,下面简单通俗分析一下当中的原 ...

  6. recv用TCP和TUDP下的区别

    recv是可以用在UDP套接字上的,前提是该套接字调用过bind或者connect,那它用在TCP和UDP套接字上时有什么区别呢? 下面做一个测试,分别使用UDP.TCP实现一对C/S.客户端发送12 ...

  7. 服务端测试环境hosts配置检查脚本

    [本文出自天外归云的博客园] 问题 由于A测试环境和B测试环境相互耦合,B测试环境切换导致我方测试环境需要更改后台服务器的响应配置.若多台服务器中有一台服务器没有更改配置,则在测试过程中将会出现问题. ...

  8. 不要问我有多懒,写个脚本跑django

    写django的都知道,要跑起django测试环境,只需要 python manage.py runserver 就可以了,但是时间一长,就觉得敲这个命令太浪费时间了,而我又是个懒人. 同事用的是ct ...

  9. django 部署,gunicorn、virtualenv、nginx

    声明: 1.本篇文章是我边写命令边写的,请尊重我的劳动成果,转载请加上链接. 2.我既然公开写出来,是希望大家遇到问题的时候有个参考,所以,大家可以免费转载,使用该文章 3.但是,如果你要用这篇文章来 ...

  10. VS2013代码调试:[7]如何避免调试时加载符号

    调试的时候不小心点了个东西,然后就花很长时间加在各种dll 解决办法: 1.点 调试2.然后 选项和设置3.右边勾上 启用源服务器支持4.左边点 符号5.把微软符号服务器勾6.运行的时候等一下 莫慌! ...