开发者

Get focus on a JTextField inside a CardLayout

开发者 https://www.devze.com 2023-03-07 16:40 出处:网络
I have a JTextField inside a JPanel A which is a part of CardLayou开发者_C百科t. When this A gets shown, I want to set the focus automatically to the JTextField (i.e. the cursor is flashing in the tex

I have a JTextField inside a JPanel A which is a part of CardLayou开发者_C百科t. 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.


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();
        }
    });
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号