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