Using mouse button events while developing sketches in the Eclipse IDE

I am developing a Processing sketch in the Eclipse IDE. I have a basic test sketch running in it. Now I need to add a menu, and in my attempt to do it I am using the information in this webpage:

https://www.processing.org/discourse/beta/num_1136371956.html

I copied and attempted to run the example code. But there were two errors. One of them I solved by adding “public” before “mouseReleased(” so that it is now:

	   addMouseListener(new MouseAdapter() {
	       public void mouseReleased(MouseEvent evt) {

This removed the error in the second line: “Cannot reduce the visibility of the inherited method from MouseAdapter” in the second line above.

There is still an error in the first line I am not sure what to do about. This is: "The method addMouseListener(new MouseAdapter(){}) is undefined for the type " I do not know what to do about this error. Will I have to create a class derived from MouseListener in a separate file, instantiate it in the first file, and qualify the addMouseListener method call by that class?

By qualify I mean, suppose the class I derive from MouseListener is “MouseClass”. Then to use it to qualify in the main file I would do something like this:

	    mymouse.addMouseListener(new MouseAdapter() {
	       public void mouseReleased(MouseEvent evt) {
1 Like

Method addMouseListener() belongs to class Component from package java.awt:
Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Component.html#addMouseListener(java.awt.event.MouseListener)

Processing’s class PApplet used to extend class Applet, which is a subclass of Component:
Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/applet/Applet.html

The latest Processing version which it was still so is v3.0a5.

After that version, most we can do is grab a Component instance like this:

import java.awt.Component;

Component win;

void setup() {
  win = (Component) getSurface().getNative();
  println(win); // processing.awt.PSurfaceAWT$SmoothCanvas[canvas0,14,14,100x100]
  exit();
}

Notice however it’s available only when we’re using the JAVA2D default renderer.

4 Likes

I have taken the info provided by @GoToLoop and the code from the old forum to produce a simple sketch that creates and displays a Java Swing popup menu in Processing 3.5.4 (i.e. latest version). As GoToLoop states this will only work with the default JAVA2D renderer.

Couple of things to point out -

  1. According to the Java documentation the adapter should catch and test both RELEASED and PRESSED events because the trigger is system dependent.
  2. Both Processing and AWT have a class called MouseEvent so we must state explicitly which class we are using.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public JPopupMenu menu = new JPopupMenu();
public Component win; 

void settings() {
  size(400, 400);
}

void setup() {
  // Get the visual component for this sketch
  win = (Component) getSurface().getNative();
  // Create and add a menu item
  JMenuItem item = new JMenuItem("Item Label 1");
  item.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      System.out.println("Item 1");
    }
  }
  );
  menu.add(item);
  item = new JMenuItem("Item Label 2");
  item.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      System.out.println("Item 2");
    }
  }
  );
  menu.add(item);
  win.addMouseListener(new MouseAdapter() {    
    // The popup trigger depends on the OS so should check 
    // both RELEASED and PRESSED
    public void mouseReleased(java.awt.event.MouseEvent evt) {
      if (evt.isPopupTrigger()) {
        menu.show(evt.getComponent(), evt.getX(), evt.getY());
      }
    }

    public void mousePressed(java.awt.event.MouseEvent evt) {
      if (evt.isPopupTrigger()) {
        menu.show(evt.getComponent(), evt.getX(), evt.getY());
      }
    }
  }
  );
}

void draw() {
  background(100, 100, 255);
}
2 Likes

Thank you for this code. It is a big help to see it all together. I migrated this code into the Eclipse IDE and it works perfectly. But due to the application I need it for some questions are raised.

The application is to display a menu of available serial ports to select from. The list of available ports can change between times the menu is displayed. This means the calls to menu.add() have to called in the draw() function. Also, to refresh the list, menu.removeAll() will have to be called, and then the calls to menu.add done all over again. Would a call to item.removeAll() also be necessary for each menu item to prevent memory leaks? Or just the one call to menu.removeAll() sufficient?

1 Like

Deal with the easy one first. In Java a memory leak is caused when an unwanted object that is no longer needed is still being referenced.

If you look at the code the object reference item is declared inside the setup so is a local variable and will be lost when the method ends. So after setup you have no reference to item so you can’t call item.removeAll() . All the ActionListeners are anonymous so no risk of memory leak there.

The only external refernence for the popup-menu menu so a call to menu.removeAll() will remove all JMenuItems objects so these and their ActionListenerers will be garbage collected therefore no memory leak.

Be very wary here, the draw method is executed about 60 times a second so you only want to rebuild the pop-menu if the list of available ports changes. I suggest that you need a mechanism to detect if there has been a change since the menu was updated, the actual way you do this is up to you.

2 Likes

Thanks for telling me where the Component is.

Do you know why it doesn’t work with the 3D renderer though?

Both P2D & P3D (a.K.a. OPENGL) renderers use the JogAmp.org library instead. :sunglasses:

1 Like

oh wait, i don’t even know which renderer my sketch is using? can i specify that somewhere?

edit: nvm, i found it, it’s specified in the size() method

so i suppose i am using the default renderer and it should work, then

Processing.org/reference/size_.html

Processing.GitHub.io/processing-javadocs/core/processing/core/PConstants.html#JAVA2D

1 Like