Also add checkmark to selected group

This commit is contained in:
SiboVG 2024-08-07 12:48:41 +02:00
parent 693f2625ba
commit 2e8accf097

View File

@ -206,7 +206,16 @@ public class SearchableAndCategorizableComboBox<E, T> extends JComboBox<T> {
// Fill the menu with the groups // Fill the menu with the groups
for (E group : itemGroupMap.keySet()) { for (E group : itemGroupMap.keySet()) {
JMenu groupList = new JMenu(group.toString()); JMenu groupMenu = new JMenu(group.toString()) {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
// If the group contains the selected item, draw a checkbox
if (containsSelectedItem(group, (T) SearchableAndCategorizableComboBox.this.getSelectedItem())) {
g.drawString("\u2713", 5, getHeight() - 5); // Unicode for checked checkbox
}
}
};
T[] itemsForGroup = itemGroupMap.get(group); T[] itemsForGroup = itemGroupMap.get(group);
if (itemsForGroup != null) { if (itemsForGroup != null) {
@ -224,11 +233,11 @@ public class SearchableAndCategorizableComboBox<E, T> extends JComboBox<T> {
itemMenu.addActionListener(e -> { itemMenu.addActionListener(e -> {
setSelectedItem(item); setSelectedItem(item);
}); });
groupList.add(itemMenu); groupMenu.add(itemMenu);
} }
} }
menu.add(groupList); menu.add(groupMenu);
} }
// Extra widgets // Extra widgets
@ -349,6 +358,18 @@ public class SearchableAndCategorizableComboBox<E, T> extends JComboBox<T> {
searchPopup.setVisible(false); searchPopup.setVisible(false);
} }
private boolean containsSelectedItem(E group, T targetItem) {
T[] itemsInGroup = itemGroupMap.get(group);
if (itemsInGroup != null) {
for (T item : itemsInGroup) {
if (item == targetItem) {
return true;
}
}
}
return false;
}
private void filter(String text) { private void filter(String text) {
filteredList.removeAll(); filteredList.removeAll();
String searchText = text.toLowerCase(); String searchText = text.toLowerCase();