Fix hiding popups after selection

This commit is contained in:
SiboVG 2024-08-08 23:04:37 +02:00
parent 9f0b7c9b15
commit 370e6bffd7

View File

@ -305,9 +305,6 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
T selectedItem = filteredList.getSelectedValue(); T selectedItem = filteredList.getSelectedValue();
if (selectedItem != null) { if (selectedItem != null) {
SearchableAndCategorizableComboBox.this.setSelectedItem(selectedItem); SearchableAndCategorizableComboBox.this.setSelectedItem(selectedItem);
fireActionEvent();
// Hide the popups after selection
hidePopups();
} }
} }
}); });
@ -340,6 +337,13 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
return targetItem != null && targetItem.getGroup().equals(group); return targetItem != null && targetItem.getGroup().equals(group);
} }
@Override
public void setSelectedItem(Object anObject) {
// Hide the popups after selection
hidePopups();
super.setSelectedItem(anObject);
}
private void filter(String text) { private void filter(String text) {
filteredList.removeAll(); filteredList.removeAll();
String searchText = text.toLowerCase(); String searchText = text.toLowerCase();
@ -376,20 +380,6 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
return categoryPopup.isVisible() || searchPopup.isVisible(); return categoryPopup.isVisible() || searchPopup.isVisible();
} }
/**
* Override the default action keys (escape, enter, arrow keys) to do our own actions.
* @param e the key event
*/
private void overrideActionKeys(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> hidePopups();
case KeyEvent.VK_ENTER -> selectHighlightedItemInFilteredList();
case KeyEvent.VK_DOWN, KeyEvent.VK_RIGHT -> highlightNextItemInFilteredList();
case KeyEvent.VK_UP, KeyEvent.VK_LEFT -> highlightPreviousItemInFilteredList();
}
}
/** /**
* Select the highlighted item in the filtered list and hide the popups. * Select the highlighted item in the filtered list and hide the popups.
*/ */
@ -405,10 +395,11 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
*/ */
private void highlightNextItemInFilteredList() { private void highlightNextItemInFilteredList() {
if (highlightedListIdx + 1 >= filteredList.getModel().getSize() || !searchPopup.isVisible()) { if (highlightedListIdx + 1 >= filteredList.getModel().getSize() || !searchPopup.isVisible()) {
highlightedListIdx++; return;
filteredList.ensureIndexIsVisible(highlightedListIdx);
filteredList.repaint();
} }
highlightedListIdx++;
filteredList.ensureIndexIsVisible(highlightedListIdx);
filteredList.repaint();
} }
/** /**
@ -558,6 +549,23 @@ public class SearchableAndCategorizableComboBox<G extends Group, T extends Group
showCategoryPopup(); showCategoryPopup();
} }
} }
/**
* Override the default action keys (escape, enter, arrow keys) to do our own actions.
* @param e the key event
*/
private void overrideActionKeys(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_ESCAPE -> hidePopups();
case KeyEvent.VK_ENTER -> selectHighlightedItemInFilteredList();
case KeyEvent.VK_DOWN -> highlightNextItemInFilteredList();
case KeyEvent.VK_UP -> highlightPreviousItemInFilteredList();
default -> {
return;
}
}
e.consume();
}
} }
private class FilteredListCellRenderer extends DefaultListCellRenderer { private class FilteredListCellRenderer extends DefaultListCellRenderer {