I have 2 JTextFields:
JTextField txtJobType, txtPriorityCode;
This is the functionality I need:
When user types in 'administration' in txtJobType and hits tab (or clicks away) an error check is done to see whether the field is empty or if the text entered exists in database. The way I have done that is:
private void txtJobTypeFocusLost(java.awt.event.FocusEvent evt) {
System.out.println("JobType Focus Lost");
if (!checkFieldExists(txtJobType.getText(), "jobType", "jobCode",
JobType.class) || txtJobType.getText().isEmpty()) {
txtJobType.requestFocusInWindow();
txtJobType.selectAll();
} else {
}
}
So if the field doesn't exist or the text is empty, then return focus to the txtJobType and highlight all the text (if any)
That works without a problem. However, I have the txtPriorityCode field which needs to have the exact same behaviour. So I did:
private void txtPriorityCodeFocusLost(java.awt.event.FocusEvent evt) {
System.out.println("PriorityCode Focus Lost");
if (!checkFieldExists(txtPriorityCode.getText(), "priority", "priorityCode",
Priority.class) || txtPriorityCode.getText().isEmpty()) {
txtPriorityCode.requestFocusInWindow();
txtPriorityCode.selectAll();
}
}
This is where the problem starts: if user leaves jobType and tabs to Priority then the code attempts to return focus back to jobtype, but because priority is also blank at that point, it will attempt to get focus back from jobtype, resulting in this output:
PriorityCode Focus Lost
JobType Focus Lost
PriorityCode Focus Lost
JobType Focus Lost
Any help on how I could implement this behaviour is appreciated, as I have to do this for at least 10 other textfields.
Thank You!