Somewhat fix menu items still being opened when not hovered over

This commit is contained in:
SiboVG 2024-08-09 01:07:39 +02:00
parent 45dcf0d81d
commit a27ab137cc

View File

@ -213,6 +213,7 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
private JPopupMenu createGroupsPopup() {
final JPopupMenu menu = new JPopupMenu();
MenuItemMouseListener menuItemListener = new MenuItemMouseListener();
// Add the search field at the top
menu.add(searchFieldGroups);
@ -226,10 +227,11 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
super.paintComponent(g);
// If the group contains the selected item, draw a checkbox
if (containsSelectedItem(group, (T) SearchableAndGroupableComboBox.this.getSelectedItem())) {
g.drawString(CHECKMARK, CHECKMARK_X_OFFSET, getHeight() - CHECKMARK_Y_OFFSET); // Unicode for checked checkbox
g.drawString(CHECKMARK, CHECKMARK_X_OFFSET, getHeight() - CHECKMARK_Y_OFFSET);
}
}
};
groupMenu.addMouseListener(menuItemListener);
List<T> itemsForGroup = itemGroupMap.get(group);
if (itemsForGroup != null) {
@ -265,6 +267,30 @@ public class SearchableAndGroupableComboBox<G extends Group, T extends Groupable
return menu;
}
private class MenuItemMouseListener extends MouseAdapter {
private JMenu currentMenu;
@Override
public void mouseEntered(MouseEvent e) {
if (e.getSource() instanceof JMenu) {
currentMenu = (JMenu) e.getSource();
currentMenu.setPopupMenuVisible(true);
}
}
@Override
public void mouseExited(MouseEvent e) {
if (currentMenu != null && !isMouseOverMenu(currentMenu, e.getPoint())) {
currentMenu.setPopupMenuVisible(false);
currentMenu = null;
}
}
private boolean isMouseOverMenu(JMenu menu, Point point) {
return menu.getBounds().contains(SwingUtilities.convertPoint(menu.getParent(), point, menu));
}
}
private JPopupMenu createSearchPopup() {
final JPopupMenu menu = new JPopupMenu();
menu.setLayout(new BorderLayout());