Если ваш контейнер получает MouseEvent, он не обработает его сразу. Вместо этого он попытается рекурсивно делегировать свою обработку одному из своих дочерних элементов. Проблема с этим подходом заключается в том, что родитель может делегировать обработку кликов дочернему элементу, который обрабатывает только движение колеса, и событие щелчка будет игнорироваться.
Вот соответствующие фрагменты из библиотеки Swing ( комментарии в основном мои)
Код: Выделить всё
// java.awt.Container#getMouseEventTargetImpl
// comp is a child of "this", it happens inside a loop
if (comp instanceof Container) {
Container child = (Container) comp;
// recursive call on a container child
Component deeper = child.getMouseEventTarget(
x - child.x,
y - child.y,
includeSelf,
filter,
searchHeavyweightDescendants);
// if a child processing MouseEvents is found, it will be the event processor
if (deeper != null) {
return deeper;
}
} else {
if (filter.accept(comp)) {
// there isn't a deeper target, but this component
// is a target
return comp;
}
}
Код: Выделить всё
// java.awt.Container.MouseEventTargetFilter
// this filter will return true when called on a MouseWheelListener
// even if the original event was about clicks
static class MouseEventTargetFilter implements EventTargetFilter {
static final EventTargetFilter FILTER = new MouseEventTargetFilter();
private MouseEventTargetFilter() {}
public boolean accept(final Component comp) {
return (comp.eventMask & AWTEvent.MOUSE_MOTION_EVENT_MASK) != 0
|| (comp.eventMask & AWTEvent.MOUSE_EVENT_MASK) != 0
|| (comp.eventMask & AWTEvent.MOUSE_WHEEL_EVENT_MASK) != 0
|| comp.mouseListener != null
|| comp.mouseMotionListener != null
|| comp.mouseWheelListener != null;
}
}
Код: Выделить всё
package demos.popup;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
/**
* Right click to trigger a popup.
*/
public class PopupDemo {
public static void main(String[] args) {
Container mainPanel = createMainPanel();
JFrame frame = new JFrame("Popup Demo");
frame.setContentPane(mainPanel);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
private static JComponent createMainPanel() {
JComponent mainComp = createPanelWithPopup();
return mainComp;
}
private static JPanel createPanelWithPopup() {
JPanel panel = new JPanel(new BorderLayout()) {
@Override
public String toString() {
return "Popup Panel";
}
};
// uncomment this to "break" the popup
// panel.add(createScrollPane());
panel.setPreferredSize(new Dimension(250,150));
panel.setComponentPopupMenu(createPopupMenu());
return panel;
}
private static JScrollPane createScrollPane() {
JScrollPane scroller = new JScrollPane();
return scroller;
}
private static JPopupMenu createPopupMenu() {
JPopupMenu popupMenu = new JPopupMenu();
popupMenu.add(createTestMenuItem());
return popupMenu;
}
private static JMenuItem createTestMenuItem() {
JMenuItem menuItem = new JMenuItem("Test menu item");
menuItem.addActionListener(e -> System.out.println("Test menu item triggered..."));
return menuItem;
}
}
Как обойти эту проблему?
Подробнее здесь: https://stackoverflow.com/questions/790 ... ellistener