JComboBox
显示单击列表。我想显示JPopupMenu
而不是列表。在下面的代码中,事件被触发,但没有显示弹出窗口。为什么?
JComboBox box = new JComboBox();
box.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
...
});
或者,可以使用mouseListener。由于JDK错误https://bugs.java.com/bugdatabase/view_bug.do?bug_id=4144505必须像这样将mouseListener添加到所有后代中:
MouseAdapter comboPopupAdapter = new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
popupMenu.show(box, 0, box.getHeight());
}
};
box.addMouseListener(comboPopupAdapter);
for (Component c : box.getComponents()) {
c.addMouseListener(comboPopupAdapter);
}