从打开的JComboBox打开JPopupMenu [英] Open JPopupMenu from opened JComboBox
问题描述
我想改变组合框的OOTB行为,在点击鼠标右键后点击冻结(检测点击哪个按钮很容易,这不是重点)并打开JPopupMenu而不是选择那个条目。
I'd like to change OOTB behaviour of combobox, to freeze it after right mouse button click (detecting which button was clicked is easy, so that's not the point) and open JPopupMenu istead of choosing that entry.
那么 - 如何禁用在给定条件下选择条目并使用自定义行为呢?
So - how to disable choosing entry on given condition and use custom behaviour then?
I试图通过向所有组合框组件添加鼠标监听器开始,但没有成功 - 没有改变
I tried to start by adding mouse listeners to all combobox components, but without success - nothing changed
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class MainClass {
public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Selecting JComboBox");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox comboBox = new JComboBox(labels);
frame.add(comboBox, BorderLayout.SOUTH);
frame.setSize(400, 200);
frame.setVisible(true);
for (Component c : comboBox.getComponents()) {
c.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("cli");
super.mouseClicked(e);
}
public void mousePressed(MouseEvent e) {
System.out.println("pre");
super.mousePressed(e);
}
});
}
}
}
推荐答案
-
在Swing中无法在同一时刻显示两个轻量级弹出容器
in Swing is not possible to showing two lightweight popup containers in the same moment
示例关于< a href =https://stackoverflow.com/a/8511560/714968>来自JPopup的JComboBox弹出窗口
有关于hack的脏话将
JPopup
设置为重量级there are dirty hack about set
JPopup
to heavyweight但我建议混合
AWT容器
withSwing JComponents
并使用AWT.Popup
withSwing .JComponent
(JMenuItem
或JButton
)but I'd suggest to mixing
AWT Container
withSwing JComponents
and to useAWT.Popup
withSwing.JComponent
(JMenuItem
orJButton
)这篇关于从打开的JComboBox打开JPopupMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!