简体   繁体   English

带有JPopupMenu的JComboBox作为右键单击选项

[英]JComboBox with JPopupMenu as right click option

So the functionality I need is the Combobox which upon expansion can be right clicked and then a popup menu shows which shows different actions. 所以我需要的功能是Combobox,可以右键单击它,然后弹出菜单显示哪些显示不同的动作。

For that I extended the JCombobox with following constructor: 为此,我使用以下构造函数扩展了JCombobox:

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

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

And created class HistoryComboPopup which extends BasicComboPopup, and has a mouselistener 并创建了一个扩展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();
    }
}

The popup works, and I can handle its menu click. 弹出窗口工作,我可以处理它的菜单点击。 The only trouble is that the combobox closes when i open JPopupMenu and item on which i clicked right click is no longer seen. 唯一的麻烦是当我打开JPopupMenu时,组合框关闭,我点击右键单击的项目不再可见。

I'm currently out of ideas what to do about this, so any help would greatly be appreciated. 我目前不知道该怎么做,所以任何帮助都将非常感激。

EDIT: i mashed up a compileable example: 编辑:我搞砸了一个可编译的例子:

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);
        }
    }
}
}

So the functionality I need is the Combobox which upon expansion can be right clicked and then a popup menu shows which shows different actions. 所以我需要的功能是Combobox,可以右键单击它,然后弹出菜单显示哪些显示不同的动作。

  • popup.show(); isn't proper accelator 是不适当的加速器

  • that not possible directly, its too hard to override common bug , 不可能直接,它太难以覆盖常见的bug

  • Swing doesn't allows to shows two lightwieght popup components in same time Swing不允许同时显示两个lightwieght弹出组件

  • use JWindows or un-decorated JDialog instead of JPopup 使用JWindows或未装饰的JDialog而不是JPopup

code example demonstrated an official 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());
                }
            });
        }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM