Setting the focus to a text field

前端 未结 9 1770
日久生厌
日久生厌 2020-12-16 14:50

I have an application developed in netbeans and I want to set the focus to a certain jTextField when a panel is displayed. I have read a number of post and have

9条回答
  •  抹茶落季
    2020-12-16 15:18

    I'm not sure if I'm missing something here, but there's no reason why you can't add a listener to your panel.

    In Netbeans, just hit the "Source" button in the top left of the editor window and you can edit most of the code. The actual layout code is mostly locked, but you can even customize that if you need to.

    As far as I'm aware, txtMessage.requestFocusInWindow() is supposed to set up the default focus for when the window is displayed the first time. If you want to request the focus after the window has been displayed already, you should use txtMessage.requestFocus()

    For testing, you can just add a listener in the constructor:

    addWindowListener(new WindowAdapter(){ 
      public void windowOpened( WindowEvent e){ 
        txtMessage.requestFocus();
      } 
    }); 
    

提交回复
热议问题