3

所以我需要的功能是组合框,在展开时可以右键单击它,然后显示一个弹出菜单,显示不同的操作。

为此,我使用以下构造函数扩展了 JCombobox:

    public HistoryComboBox(DefaultComboBoxModel model) {
    super(model);
    super.setUI(new BasicComboBoxUI(this){

        @Override protected ComboPopup createPopup() {
            return new HistoryComboPopup(comboBox);
        }
    });

并创建了扩展 BasicComboPopup 的 HistoryComboPopup 类,并有一个 mouselistener

@Override
protected MouseListener createListMouseListener() {
    if (handler2 == null)
        handler2 = new Handler2();
    return handler2;
}

private class Handler2 implements MouseListener {
    @Override
    public void mouseClicked(MouseEvent e) {
        JPopupMenu popup = new JPopupMenu();
        ...
        popup.show();
    }
}

弹出窗口有效,我可以处理它的菜单点击。唯一的问题是当我打开 JPopupMenu 时组合框关闭,并且不再看到我单击右键的项目。

我目前不知道该怎么做,所以任何帮助将不胜感激。

编辑:我混搭了一个可编译的例子:

public class MainPanel extends JPanel{
public MainPanel() {
    super(new BorderLayout());

    JComboBox combo1 = makeComboBox(5);
    combo1.setUI(new BasicComboBoxUI() {
        @Override protected ComboPopup createPopup() {
            return new HistoryComboPopup(comboBox);
        }
    });

    add(combo1,BorderLayout.NORTH);
}

private static JComboBox makeComboBox(int size) {
    DefaultComboBoxModel model = new DefaultComboBoxModel();
    for(int i=0;i<size;i++) {
        model.addElement("No."+i);
    }
    return new JComboBox(model);
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override public void run() {
            JFrame frame = new JFrame("DisableRightClick");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.getContentPane().add(new MainPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}
}

class HistoryComboPopup extends BasicComboPopup {
private Handler2 handler2;

@Override
public void uninstallingUI() {
    super.uninstallingUI();
    handler2 = null;
}

public HistoryComboPopup(JComboBox combo) {
    super(combo);
}

@Override
protected MouseListener createListMouseListener() {
    if (handler2 == null)
        handler2 = new Handler2();
    return handler2;
}

private class Handler2 implements MouseListener {
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e)) {
            JPopupMenu popup = new JPopupMenu();

            JMenuItem copymethod = new JMenuItem("copy");
            copymethod.addMouseListener(new MouseAdapter() {

                @Override
                public void mousePressed(MouseEvent e) {
                    System.out.println("copy clicked");
                }
            });
            popup.add(copymethod);
            popup.show(HistoryComboPopup.this, 
                    e.getXOnScreen() - HistoryComboPopup.this.getLocationOnScreen().x, 
                    e.getYOnScreen() - HistoryComboPopup.this.getLocationOnScreen().y);

        } else {
            comboBox.setSelectedIndex(list.getSelectedIndex());
            comboBox.setPopupVisible(false);
        }
    }
}
}
4

1 回答 1

1

所以我需要的功能是组合框,在展开时可以右键单击它,然后显示一个弹出菜单,显示不同的操作。

  • popup.show();不是合适的加速器

  • 这不可能直接,它很难覆盖常见的错误

  • Swing 不允许同时显示两个轻量级的弹出组件

  • 使用 JWindows 或未修饰的 JDialog 而不是 JPopup

代码示例演示了一个官方错误

    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());
                }
            });
        }
   }
于 2013-07-09T13:29:55.800 回答