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 tried various methods but non have worked. One of the main issues is where to place the required code, which I believe in my case is this.txtMessage.requestFocusInWindow();
There are some posts that indicate using a Window Listener, however as netbeans has generated the GUI, I cannot see how to implement the interfaces as I cannot edit the code that creates the jPANEL etc. The whole thing is very frustrating and I really do not believe that this should be that difficult.
Just as a test I added the requestFocusInWindow();
to a button on the panel and it did set the focus to the desired input.
For me the easiest way to get it to work, is to put the component.requestFocus();
line, after the setVisible(true);
line, at the bottom of your frame or panel constructor.
This probably has something to do with asking for the focus, after all components have been created, because creating a new component, after asking for the focus request, will make your component loose te focus, and make the focus go to your newly created component. At least, that's what I assume.
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();
}
});
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 have toyed with this for forever, and finally found something that seems to always work!
textField = new JTextField() {
public void addNotify() {
super.addNotify();
requestFocus();
}
};
None of the above worked for me, because my window is a JPopupMenu
.
What did work was this:
addAncestorListener(new AncestorListener() {
@Override
public void ancestorAdded(AncestorEvent ae) {
myEdit.requestFocus();
}
// ... other ancestor listener methods
}
This is an easy one:
textField.setFocus();
I did it by setting an AncesterAdded event on the textField and the requesting focus in the window.
In a JFrame or JDialog you can always overwrite the setVisible() method, it works well. I haven't tried in a JPanel, but can be an alternative.
@Override
public void setVisible(boolean value) {
super.setVisible(value);
control.requestFocusInWindow();
}
Source: Stackoverflow.com