Open JPopupMenu from opened JComboBox

◇◆丶佛笑我妖孽 提交于 2020-01-11 10:34:25

问题


I'd like to change OOTB behaviour of combobox, to freeze it after right mouse button click (detecting which button was clicked is easy, so that's not the point) and open JPopupMenu istead of choosing that entry.

So - how to disable choosing entry on given condition and use custom behaviour then?

I tried to start by adding mouse listeners to all combobox components, but without success - nothing changed

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class MainClass {

    public static void main(final String args[]) {

        final String labels[] = { "A", "B", "C", "D", "E" };
        JFrame frame = new JFrame("Selecting JComboBox");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JComboBox comboBox = new JComboBox(labels);
        frame.add(comboBox, BorderLayout.SOUTH);
        frame.setSize(400, 200);
        frame.setVisible(true);

        for (Component c : comboBox.getComponents()) {
            c.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    System.out.println("cli");
                    super.mouseClicked(e);
                }
                public void mousePressed(MouseEvent e) {
                    System.out.println("pre");
                    super.mousePressed(e);
                }
            });
        }
    }
}

回答1:


  • in Swing is not possible to showing two lightweight popup containers in the same moment

  • example about JComboBox popup from JPopup

  • there are dirty hack about set JPopup to heavyweight

  • but I'd suggest to mixing AWT Container with Swing JComponents and to use AWT.Popup with Swing.JComponent (JMenuItem or JButton)



来源:https://stackoverflow.com/questions/11245982/open-jpopupmenu-from-opened-jcombobox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!