JPopupMenu上的JComboBox [英] JComboBox on a JPopupMenu
问题描述
我正在尝试使用复合的Swing组件作为菜单的一部分.
I'm trying to use a compound Swing component as part of a Menu.
除了一个细节外,一切工作都很好:该组件包含JComboBox
es,并且每当用户单击其中的一个以打开其下拉列表时,下拉列表都会打开,但菜单会消失.单击JComboBox
能否使菜单保持打开状态?
Everything works just fine, apart from one detail: The component contains JComboBox
es and whenever the user clicks on one of them to open its dropdown, the dropdown opens but the menu disappears. Is it possible to make the menu stay open when a JComboBox
is clicked?
我将其分类为JMenu
.这是对应的代码:
I sub-classed JMenu
. This is the corresponding code:
public class FilterMenu extends JMenu {
public FilterMenu(String name) {
super(name);
final JPopupMenu pm = this.getPopupMenu();
final FilterPanel filterPanel = new FilterPanel(pm) {
@Override
public void updateTree() {
super.updateTree();
pm.pack();
}
};
pm.add(filterPanel);
}
}
FilterPanel
是自定义复合组件.当filterPanel
的大小改变时,调用pm.pack()
来适应JPopupMenu
的大小.
FilterPanel
is the custom compound component. The pm.pack()
is called to adapt the size of the JPopupMenu
when the filterPanel
changes in size.
感谢您的帮助
推荐答案
您的意思是此错误
import javax.swing.*;
import java.awt.event.*;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setVisible(true);
String[] list = {"1", "2", "3", "4",};
JComboBox comb = new JComboBox(list);
final JPopupMenu pop = new JPopupMenu();
pop.add(comb);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
System.out.println("mousePressed");
pop.show(e.getComponent(), e.getX(), e.getY());
}
});
}
}
这篇关于JPopupMenu上的JComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!