Get focus on a JTextField inside a CardLayout

佐手、 提交于 2019-12-23 22:44:28

问题


I have a JTextField inside a JPanel A which is a part of CardLayout. When this A gets shown, I want to set the focus automatically to the JTextField (i.e. the cursor is flashing in the text field so the user doesn't need to click on it to enable the input). I tried calling requestFocusInWindow() on the JTextField object at initialization, but that doesn't seem to work. Do I need to call this method every time when A gets displayed? Thanks.


回答1:


Maybe you can try requestFocusInWindow() when the panel is shown ?

something like this?

    jPanel.addComponentListener(new ComponentAdapter() {
        @Override 
        public void componentShown(java.awt.event.ComponentEvent e) 
        {
            jTextField.requestFocusInWindow();
        }
    });


来源:https://stackoverflow.com/questions/6066642/get-focus-on-a-jtextfield-inside-a-cardlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!