How do I make a JPopupMenu appear when any one of

2019-07-31 07:23发布

I am trying to make a set of navigation buttons for a file browser. I want it so that if the user clicks the dedicated history button, a JPopupMenu appears. However, I also want that exact same menu to appear when the user right-clicks or drags the cursor down the back or forward button. How can I make that exact same JPopupMenu (not a copy, but the same exact one) appear for multiple GUI components for different gestures?

So far I've tried the following:

histButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});
forwardButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    if (e.isPopupTrigger())
      showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});
backButton.addMouseListener(new MouseAdapter()
{
  @Override public void mouseClicked(MouseEvent e)
  {
    if (e.isPopupTrigger())
      showPopup(e);
  }

  @Override public void mouseDragged(MouseEvent e)
  {
    showPopup(e);
  }

  private void showPopup(MouseEvent e)
  {
    histPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  }
});

All components are added and display correctly, and debugging shows me that they register the events, but no menu appears.

1条回答
Luminary・发光体
2楼-- · 2019-07-31 08:00

Bringing Up a Popup Menu shows the traditional implementation using mousePressed(), mouseReleased() and isPopupTrigger(). Note that "The exact gesture that should bring up a popup menu varies by look and feel." You might compare what's shown with your implementation, which uses mousePressed().

Addendum: For reference, @mKorbel recalls this client property that may prove useful.

import java.awt.Component;
import java.awt.event.*;
import javax.swing.*;

/** @author mKorbel */
public class ComboBoxAction extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;
    private JComboBox comboBox;
    private JFrame frame;

    public ComboBoxAction() {
        comboBox = new JComboBox();
        comboBox.addActionListener(this);
        comboBox.addItem("Item 1");
        comboBox.addItem("Item 2");
        comboBox.addItem("Item 3");
        comboBox.addItem("Item 4");
        for (Component component : comboBox.getComponents()) {
            if (component instanceof AbstractButton) {
                if (component.isVisible()) {
                    comboBox.remove(component);
                }
            }
        }
        //This prevents action events from being fired when the
        //up/down arrow keys are used on the dropdown menu
        comboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
        comboBox.firePopupMenuWillBecomeVisible();
        frame = new JFrame();
        frame.add(comboBox);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(comboBox.getSelectedItem());
        //make sure popup is closed when 'isTableCellEditor' is used
        comboBox.hidePopup();
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ComboBoxAction();
            }
        });
    }
}
查看更多
登录 后发表回答