我正在尝试使用复合Swing组件作为菜单的一部分.
一切工作正常,除了一个细节:该组件包含JComboBoxes,每当用户点击其中一个打开其下拉列表时,下拉列表打开但菜单消失.单击JComboBox时是否可以使菜单保持打开状态?
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是自定义复合组件.调用pm.pack()以在filterPanel大小改变时调整JPopupMenu的大小.
谢谢你的帮助
解决方法
你的意思是
this bug
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()); } }); } }