Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4245285

ability to use addMouseListener with JComboBox

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.1
    • client-libs



      Name: skT88420 Date: 06/09/99


      Adding a mouse listener to a JComboBox does not appear to work.
      I susect this has to do with having a render Component which
      is really getting the mouse events wither than the JComboBox.
      Here is a simple piece of code. The popup menu appears over
      the JTextField, but not the JComboBox. This problem also
      occurs with the Solaris Production JDK.

      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JComboBox;
      import javax.swing.JPopupMenu;
      import javax.swing.JTextField;
      import java.awt.event.MouseEvent;
      import java.awt.event.MouseAdapter;

      public class test extends JPanel {

          JPopupMenu p = null;

          class PopupAdapter extends MouseAdapter {
              public void mousePressed(MouseEvent e) {
                  if(e.isPopupTrigger()) {
                      p.show(e.getComponent(), e.getX(), e.getY());
                  }
              }
          }

          public test () {
              JComboBox b = new JComboBox();
              b.addItem("abc");
              b.addItem("def");
              add(b);
              JTextField t = new JTextField(10);
              add(t);
              p = new JPopupMenu();
              p.add("hij");
              p.add("klm");
              PopupAdapter pa = new PopupAdapter();
              b.addMouseListener(pa);
              t.addMouseListener(pa);
          }

          static public void main(String args[]) {
              JFrame f = new JFrame("test");
              test t = new test();
              f.setContentPane(t);
              f.pack();
              f.show();
          }
      }
      (Review ID: 84156)
      ======================================================================

            tsantossunw Tom Santos (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: