2 Replies Latest reply on Aug 3, 2003 10:28 PM by 807577

    JPopupMenu or mouseReleased problem

    807577
      Java rookie here so bear with me. Trying out a swing popupmenu by right clicking I get:
      java.lang.NullPointerException
      at PresentingLife$MouseWatcher.mouseReleased(PresentingLife.java:580)
      ...
      every time I release the mouse button in my jpanel and the popup menu never appears, but the cordinates do. I'm sure I'm missing something small.
      Here's all the code that applies to:

      JPopupMenu popup1 = new JPopupMenu();
      popup1.setVisible(true);

      JMenuItem item = new JMenuItem("Add Image");
           item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, Event.CTRL_MASK));
      item.setMnemonic(KeyEvent.VK_I);
           popup1.add(item);
           item.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      addImageMenuItemActionPerformed(evt);
      }
      });

           item = new JMenuItem("Add Text");
           item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, Event.CTRL_MASK));
           item.setMnemonic(KeyEvent.VK_T);
           popup1.add(item);
           item.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      addTextMenuItemActionPerformed(evt);
      }
      });

      item = new JMenuItem("Delete Image");
           item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, Event.CTRL_MASK));
           item.setMnemonic(KeyEvent.VK_T);
           popup1.add(item);
           item.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
      deleteImageMenuItemActionPerformed(evt);
      }
      });

      MouseListener mouser = new MouseWatcher();
                jPanel4.addMouseListener(mouser);
                addMouseListener(mouser);
      ...

      private class MouseWatcher extends MouseAdapter
           {     public void mouseClicked(MouseEvent e)
                {     Graphics g = e.getComponent().getGraphics();
                     int x = e.getX(), y = e.getY();
                     if(e.getModifiers() == InputEvent.BUTTON3_MASK)
                     {     Font font = new Font("Serif",  Font.ITALIC, 10);
                          g.setFont(font);
                          g.setColor(Color.red);
                          g.drawString("Right Click at:(" + x + "," + y + ")", x, y);
                     }
                     else // Other buttons.
                     {     g.drawString("Left Clickat:(" + x + "," + y + ")", x, y);     
                     }
                     g.dispose();
                }
                
                public void mousePressed(MouseEvent e)
                {     int x = e.getX(), y = e.getY();
                     if (e.isPopupTrigger())
                     {     popup1.show(e.getComponent(), x, y);     
                     }
                }

                public void mouseReleased(MouseEvent e)
                {     int x = e.getX(), y = e.getY();
                     if (e.isPopupTrigger())
                     {     popup1.show(e.getComponent(), x, y);     
                     }          
                }
           }

      Thank You