How do I make a JPopupMenu appear when any one of 3 JButtons is clicked, right-clicked, or dragged upon?

微笑、不失礼 提交于 2020-01-15 11:59:05

问题


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:


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


来源:https://stackoverflow.com/questions/10860648/how-do-i-make-a-jpopupmenu-appear-when-any-one-of-3-jbuttons-is-clicked-right-c

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