I have a number of components in my window, most of them JTextFields. To save the user time in entering data, I am trying to parse the contents of the first JTextField when populated, and put suggested values into subsequent JTextFields.
I have added a FocusListener to the first JTextField, and in focusLost() I retrieve its contents and then set the contents of the subsequent JTextFields. This works correctly the first time the application is used. However on subsequent tasks (i.e. after all the data has been processed and the fields cleared) it usually (but not always) fails. Debugging is difficult with focus issues so I have resorted to logging instead and can see that in the failing cases the getText() method on the first JTextField is returning null. This must be a timing issue but I cannot see a way round it.
The focusLost() method begins...
@Override
public void focusLost(FocusEvent e) {
// On losing focus of the title field attempt to populate all the other
// resource detail fields
Component rawComponent = e.getComponent();
if (this.resourceTitleTextField == rawComponent) {
JTextField component = (JTextField) rawComponent;
String title = component.getText();
LOGGER.info("title = " + title);