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
I did it by setting an AncesterAdded event on the textField and the requesting focus in the window.
If you create your GUI with Netbeans, you can also insert some self written code. Just select an element (maybe the button, panel or the window) and use the "Code"-tab in the "Properties"-dialog.
There you can insert Pre- and Post- code for various parts of the creation process.
I think the "After-All-Set-Code" field of the window is a good place for your code, or you could bind it to the event ("Properties"-dialog -> "Events") "componentShown" of the text field / panel.
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();
}
});