JavaFX Unable to add modules to Processing IDE

The following JavaFX code will not run in Processing 4 due to absence of JavaFX modules (runs ok in version 3.5.4 minus the first line). I have tried many times adding the modules to the libraries folder or drag 'n dropping them on the sketch, but neither have worked. Text and graphics work fine with the FX2D renderer, but the controls won’t work. It is possible to create graphic buttons and trap mouse clicks or use SwingNode and add Swing components, but it just seems like we should be able to use official JavaFX controls. It is unclear to me why the requisite modules can not be added to the current Processing IDE.

Non-functional demo (see console error message)

import processing.javafx.*;  // Add this for Processing 4:
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

void setup() {  
  size(1, 1, FX2D); 
  Stage stage = new Stage();
  stage.setTitle("Button demo");
  Button btn = new Button("button");
  StackPane pane = new StackPane();
  pane.getChildren().add(btn);
  Scene scene = new Scene(pane, 200, 200);
  stage.setScene(scene);
  stage.show();  
}

void draw() {
}

A related GitHub issue:

:)

Thanks. I’ve seen the github messages, but am looking for ways to solve the problem, which shouldn’t be that difficult in my opinion. Just a few observations:

  1. With Processing 4 the code without a control will run just fine without the first line (import processing.javafx.*) indicating to me that Processing 4 is able to get requisite files from someplace else instead of the libraries folder in Documents/Processing.

  2. When the first line of code is included in Processing 3.5.4 the first thing that happens is an error dialog which says that more than one library is competing for the sketch and one of them has to be removed. The two locations are the libraries javafx folder and the other is in the app bundle. Removing the first line of code in 3.5.4 then allows the sketch to run, so at least for Processing 3.4.5 it appears to be using the libraries in the app bundle.

Therefore, it would seem reasonable to me to try adding the control modules to the app bundle of Processing 4 since my efforts of adding them to the libraries folder (Documents/Processing/libraries/javafx) have failed. I’m doing this on a Mac and from previous experience I have seen that if you alter the bundle on a code-signed app the gatekeeper will automatically disable the app so that it will no longer run. Nonetheless, I’m willing to try knowing that I’ll probably trash Processing 4 in the process, but can just download a new copy if necessary. The experiment would best be done by the Processing team; they could add the libraries to the app bundle and then code-sign the app prior to distribution which should not present a problem. Unfortunately I don’t know how to compile and code-sign the open source code for the Processing IDE. I think it would have to be an Xcode project in order for me to do it without trashing the editor.

Followup:

I just copy/pasted a javafx folder from another app (BlueJ) into the Processing 4 bundle and it accepted it without a problem; the app still runs as expected. This folder contains all the jar files and libraries necessary to add javafx controls to a Processing app; folder contents shown below. Now if we could just recode the Processing IDE to use the folder we should be able to use javafx controls.

Smoking gun?

https://github.com/processing/processing4/blob/3185ebae15f1ee894a9a2a056cc253b4677012e3/java/src/processing/mode/java/JavaBuild.java

line 1079:

 static public String[] getArgsJavaFX(String modulePath) {

    return new String[] {

      "--module-path", modulePath,




      // Full list of modules, let's not commit to all of these unless

      // a compelling argument is made or a reason presents itself.

      //"javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.media,javafx.swing,javafx.web"

      "--add-modules", "javafx.base,javafx.graphics,javafx.swing",




      // TODO Presumably, this is only because com.sun.* classes are being used?

      // https://github.com/processing/processing4/issues/208

      "--add-exports", "javafx.graphics/com.sun.javafx.geom=ALL-UNNAMED",

      "--add-exports", "javafx.graphics/com.sun.glass.ui=ALL-UNNAMED"

    };

  }

Note that javafx.controls didn’t make the list.

Modules folder:

As it turns out all the requisite jar files and libraries for javafx live here on my system; no need to import them:

But they do have to be hooked up as shown above.

Hello @svan,

If you have access to a W10 PC consider doing a build with the changes you have in mind.

A link to building is here:

If you provide clear instructions on what to add\modify there may be interest in trying this for W10.

If time permits I may try it over the weekend…

:)

I greatly appreciate your interest and help with this (and other problems in the forum).

What to modify:

Start with the link that you provided above.

  1. Open the “java” folder.
  2. Open the “src/processing/mode/java” folder.
  3. Open the file “JavaBuild.java”
  4. Go to line 1086

change:
“–add-modules”, “javafx.base,javafx.graphics,javafx.swing”,

to:
“–add-modules”, “javafx.base,javafx.graphics,javafx.swing,javafx.controls”,

This should allow us to use javafx.controls in our Processing javafx projects. The way it is now we are unable to do this in Processing 4 as you are aware. Interesingly we can use SwingNode to add Swing controls (because they did add the javafx.swing module) but these controls are really ugly with a rectangular halo when initially used. This can be worked around by using a system call to set a transparent background and then jostling the frame to cause a refresh, but this is simply making it more difficult than it has to be and would be solved by using javafx controls to start with.

Why I think it will work:

If you try to compile/run pure javafx files from the command line you have to use “javac” and “java” with the “–add-modules” option added to both. If the correct modules are not added the code fails. For example:

To compile:
javac --module-path javafx-sdk-18.0.2/lib/ --add-modules javafx.controls,javafx.graphics,javafx.media,javafx.fxml ClickMe.java

and to run:
java --module-path javafx-sdk-18.0.2/lib/ --add-modules javafx.controls,javafx.graphics,javafx.media ClickMe

I believe that line 1086 determines what is inserted for the --add-modules option when our code is compiled and ran in Processing 4.

It does work!!

I followed the build instructions referenced in the link above (hardest part was getting apache-ant installed on Mac); after making the modifications to line 1086 of JavaBuild.java file outlined above the following code ran without error. Beautiful javafx button output is shown below that. I hope that the same modification will be made to Processing4 so that others can use these controls. Since Processing doesn’t have its own widgets I think it would be a nice addition for those that use controls in their projects.

Source code:

import processing.javafx.*;  // Add this for Processing 4:
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

void setup() {  
  size(1, 1, FX2D); 
  Stage stage = new Stage();
  stage.setTitle("Button demo");
  Button btn = new Button("button");
  btn.setLayoutX(80);
  btn.setLayoutY(80);
  Pane pane = new Pane();
  pane.getChildren().add(btn);
  Scene scene = new Scene(pane, 250, 200);
  stage.setScene(scene);
  stage.show();  
}

void draw() {
}

Output:
javafx_btn

Special thanks to @glv for valuable assistance.

2 Likes

The revised version of Processing4 is in the processing4/build/macos/work folder on my system. The processing4 folder was created by a github clone of the source code for the Processing4 editor. In order to continue to use the revised version it needs to be placed in the Applications folder on a Mac, thereby replacing the original distributed version.

And the adventure continues…

:)