1

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.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 1
    [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Jun 02 '12 at 07:37
  • Good, but for better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 02 '12 at 08:05
  • 1
    Not enough info. What's inside your histPopupMenu? Again, SSCCE please! – jaselg Jun 02 '12 at 08:43
  • 1
    @Supuhstar 1) your concept can frustrting user 2) is possible that, but not this way (bunch of code required for override LeftMouseButton) 3) post an SSCCE 4) JFileBrowres is compound JComponent, you can extract JPanel with implemented JButtons and/or add/replace with own 5) maybe JPopup isn't right container for popup window – mKorbel Jun 02 '12 at 11:12

1 Answers1

2

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();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • +1 why not put there JComboBox with removed Button from BasicComboBoxUI, basically is possible to setLocation for derived Popup into BasicComboBoxUI too, just idea – mKorbel Jun 02 '12 at 13:01
  • @mKorbel: Interesting. Am I correct to infer that this is similar to your comment [here](http://stackoverflow.com/a/3008587/230513) and your +1 suggestion [here](http://stackoverflow.com/a/10861923/230513)? – trashgod Jun 02 '12 at 14:59
  • basically you are right, I'll search (un_complete) for code in my PC – mKorbel Jun 02 '12 at 15:06
  • please apologize for my edit to your post, please to revert after reading (rest could be to re_layout JComboBox, and coloring with arrays of Colors from current JButtons Gradient :-)) – mKorbel Jun 02 '12 at 15:33
  • This appears to be for the popup from a combo box. I want one for a JPopupMenu. – Ky - Jun 04 '12 at 23:13
  • Right; I took it to be an alternative to `JPopupMenu`. I don't think this [example](http://stackoverflow.com/a/5129757/230513), which uses `setComponentPopupMenu()`, will do. – trashgod Jun 05 '12 at 04:15