void (ActionEvent e) { // if focus is on the popup: select the very first item on the // popup, changing probably from the choices list to the history // list; if the item is already on the very first element, or // the focus is on the text field, move the caret home if (!popup.isPopupFocused() || !popup.selectFirst(true)) { if (e.getSource() instanceof JTextField textField) { textField.setCaretPosition(0); } } }