Reputation: 15
I have 4 JTextfields in my java swing form. The problem is I need to move the Focus from one JTextField to other through java code not by using tab key.
If the Focus gained by JTextField2 means the content in the JTextField2 need to be selected. I don't know how to do this plz put your proper code associate with this issue
Upvotes: 1
Views: 7753
Reputation: 109815
that could be little bit complicated
you have to wrap and delay your Action or ActionListener into invokeLater()
, and put inside (most safiest way is to set there follows code lines)
JTextField2.setText(JTextField2.getText);
and
JTextField2.selectAll();
edit to @Andrew Thompson
private FocusListener fcsListener = new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
dumpInfo(e);
}
@Override
public void focusLost(FocusEvent e) {
//dumpInfo(e);
}
private void dumpInfo(FocusEvent e) {
System.out.println("Source : " + name(e.getComponent()));
System.out.println("Opposite : " + name(e.getOppositeComponent()));
System.out.println("Temporary: " + e.isTemporary());
Component c = e.getComponent();//works for editable JComboBox too
if (c instanceof JFormattedTextField) {
((JFormattedTextField) c).selectAll();
} else if (c instanceof JTextField) {
((JTextField) c).selectAll();
}//both methods not correct required setText(getText()) inside invokeLater
}
private String name(Component c) {
return (c == null) ? null : c.getName();
}
};
Upvotes: 1
Reputation: 128807
You can call requestFocusInWindow()
for the textfield you want focus.
Upvotes: 1