restore last combo box selection on focus out

This commit is contained in:
woodser 2025-07-12 10:43:23 -04:00 committed by GitHub
parent 8f505ab17b
commit 10347ae488
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 38 additions and 9 deletions

View file

@ -46,6 +46,7 @@ public class AutocompleteComboBox<T> extends JFXComboBox<T> {
private List<T> matchingList;
private JFXComboBoxListViewSkin<T> comboBoxListViewSkin;
private boolean selectAllShortcut = false;
private T lastCommittedValue;
public AutocompleteComboBox() {
this(FXCollections.observableArrayList());
@ -59,6 +60,30 @@ public class AutocompleteComboBox<T> extends JFXComboBox<T> {
fixSpaceKey();
setAutocompleteItems(items);
reactToQueryChanges();
// Store last committed value so we can restore it if nothing selected
valueProperty().addListener((obs, oldVal, newVal) -> {
if (newVal != null)
lastCommittedValue = newVal;
});
// Restore last committed value when editor loses focus if no matches
getEditor().focusedProperty().addListener((obs, wasFocused, isNowFocused) -> {
if (!isNowFocused) {
String input = getEditor().getText();
T matched = getConverter().fromString(input);
boolean matchFound = getItems().stream()
.anyMatch(item -> item.equals(matched));
if (!matchFound) {
UserThread.execute(() -> {
getSelectionModel().select(lastCommittedValue);
getEditor().setText(asString(lastCommittedValue));
});
}
}
});
}
/**
@ -100,10 +125,16 @@ public class AutocompleteComboBox<T> extends JFXComboBox<T> {
return;
}
// Case 2: fire if the text is empty to support special "show all" case
// Case 2: fire if the text is empty
if (inputText.isEmpty()) {
eh.handle(e);
getParent().requestFocus();
// Restore the last committed value
UserThread.execute(() -> {
getSelectionModel().select(lastCommittedValue);
getEditor().setText(asString(lastCommittedValue));
});
}
});
}

View file

@ -501,6 +501,8 @@ public class TraditionalAccountsView extends PaymentAccountsView<GridPane, Tradi
}
});
paymentMethodComboBox.setOnChangeConfirmed(e -> {
if (paymentMethodComboBox.getEditor().getText().isEmpty())
return;
if (paymentMethodForm != null) {
FormBuilder.removeRowsFromGridPane(root, 3, paymentMethodForm.getGridRow() + 1);
GridPane.setRowSpan(accountTitledGroupBg, paymentMethodForm.getRowSpan() + 1);

View file

@ -116,15 +116,11 @@ public class TradesChartsView extends ActivatableViewAndModel<VBox, TradesCharts
if (comboBox.getItems().isEmpty())
return null;
if (query.isEmpty())
return specialShowAllItem();
return null;
return comboBox.getItems().stream().
filter(currencyItem -> currencyItem.codeDashNameString().equals(query)).
findAny().orElse(null);
}
private CurrencyListItem specialShowAllItem() {
return comboBox.getItems().get(0);
}
}
private final User user;
@ -291,7 +287,7 @@ public class TradesChartsView extends ActivatableViewAndModel<VBox, TradesCharts
currencyComboBox.setOnChangeConfirmed(e -> UserThread.execute(() -> {
if (currencyComboBox.getEditor().getText().isEmpty())
currencyComboBox.getSelectionModel().select(SHOW_ALL);
return;
CurrencyListItem selectedItem = currencyComboBox.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
model.onSetTradeCurrency(selectedItem.tradeCurrency);

View file

@ -347,7 +347,7 @@ abstract public class OfferBookView<R extends GridPane, M extends OfferBookViewM
currencyComboBox.setOnChangeConfirmed(e -> {
if (currencyComboBox.getEditor().getText().isEmpty())
currencyComboBox.getSelectionModel().select(SHOW_ALL);
return;
model.onSetTradeCurrency(currencyComboBox.getSelectionModel().getSelectedItem());
paymentMethodComboBox.setAutocompleteItems(model.getPaymentMethods());
model.updateSelectedPaymentMethod();
@ -500,7 +500,7 @@ abstract public class OfferBookView<R extends GridPane, M extends OfferBookViewM
if (comboBox.getItems().isEmpty())
return null;
if (query.isEmpty())
return specialShowAllItem();
return null;
return comboBox.getItems().stream().
filter(item -> asString(item).equals(query)).
findAny().orElse(null);