如何将JComboBox弹出菜单列表作为组件对象

时间:2018-02-02 12:16:25

标签: java swing listener jcombobox popupmenu

在我的swing应用程序中,我有一个带有Combobox的对话框。在我正在应用AffineTransformation。所有组件都在调整大小,但JComponent弹出菜单列表没有缩放。我正在尝试使用一个事件来获取ComboBox的Popupmenu对象。

 public static void displayComp(JComponent jComponent) {
    if (jComponent != null) {
        final Container awtContainer = (Container) jComponent;
        final Component[] components = awtContainer.getComponents();
        for (Component comp : components) {
            if (comp instanceof JComboBox) {

                JComboBox<JComponent> b = (JComboBox<JComponent>) comp;
                b.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("e::" + e);
                        JMenuItem mi = (JMenuItem) e.getSource();
                        JPopupMenu popup = (JPopupMenu) mi.getParent();
                        System.out.println("popup:::" + popup);
                    }
                });
                System.out.print("JComboBox :::");
                System.out.println(comp.getPreferredSize() + "***Component**" + comp.getName());
            }

            if (comp instanceof JComponent) {
                displayComp((JComponent) comp);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取弹出组件

JComboBox combo = ...;
BasicComboPopup popup = (BasicComboPopup) combo.getUI().getAccessibleChild(combo, 0);

答案 1 :(得分:0)

当你的组合框不是JCombobox的孩子时显示的JPopupMenu。所以AffineTransformation不适用于它。