How can I popup a menu on right click on JComboBox?

Sandip Chitale

First off - you should not show the popup menu on right mouse click. That is because the standard for which mouse gesture should show the popup menu is different on different platform. That is why the MouseEvent class has a method called isPopupTrigger(). You should show the popup menu if this method returns true.

Here is the code -


final JComboBox combobox = // create combobox;
final JPopupMenu popup = new JPopupMenu();
// add menuitems to it and hook up action
// listeners to the menuitems
combobox.addMouseListener(
  new MouseAdapter() {
    public void mousePressed(MouseEvent me) {
      if (me.isPopupTrigger()) {
         showPopup(me);
      }
    }
    public void mouseReleased(MouseEvent me) {
      if (me.isPopupTrigger()) {
         showPopup(me);
      }
    }
    void showPopup(MouseEvent me) {
      popup.show(combobox, me.getX(), me.getY());
    }
  }
);

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact