6

I have some issues with javafx and org.controlsfx.control.textfield.TextFields. I'm trying to implement a feature that would get possible user input predictions from a database so that user can only pick the "authorized" options. While working with controlsfx I came across this error.

Error:

Exception in thread "JavaFX Application Thread" java.lang.IllegalAccessError: class org.controlsfx.control.textfield.AutoCompletionBinding (in unnamed module @0x239a4ba) cannot access class com.sun.javafx.event.EventHandlerManager (in module javafx.base) because module javafx.base does not export com.sun.javafx.event to unnamed module @0x239a4ba
at org.controlsfx.control.textfield.AutoCompletionBinding.<init>(AutoCompletionBinding.java:521)
at impl.org.controlsfx.autocompletion.AutoCompletionTextFieldBinding.<init>(AutoCompletionTextFieldBinding.java:107)
at impl.org.controlsfx.autocompletion.AutoCompletionTextFieldBinding.<init>(AutoCompletionTextFieldBinding.java:92)
at org.controlsfx.control.textfield.TextFields.bindAutoCompletion(TextFields.java:187)
at sample.Controller.lambda$listenKey$0(Controller.java:40)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$KeyHandler.process(Scene.java:4058)
at javafx.graphics/javafx.scene.Scene$KeyHandler.access$1500(Scene.java:4004)
at javafx.graphics/javafx.scene.Scene.processKeyEvent(Scene.java:2121)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2595)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$1(GlassViewEventHandler.java:248)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at javafx.graphics/com.sun.glass.ui.View.handleKeyEvent(View.java:547)
at javafx.graphics/com.sun.glass.ui.View.notifyKey(View.java:971)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)

I've learnt that, quote Since Java Web Start is no longer a part of Java SE 11 and later, we will not look into this.. I don't know if it's because of that or I'm doing something wrong.

Here's my code:

    package sample;
/**
 * Sample Skeleton for 'sample.fxml' Controller Class
 */

import java.net.URL;
import java.util.*;

import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import org.controlsfx.control.textfield.TextFields;

public class Controller{

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    // fx:id="inputAutoComplete";
    @FXML
    private TextField inputAutoComplete;

    @FXML // This method is called by the FXMLLoader when initialization is complete
    void initialize() {
        System.out.println("XD");
        listenKey();
    }

    ArrayList<String> possibleWordSet = new ArrayList<>();

    public void listenKey(){
        databaseConnection dbconn = new databaseConnection();
        inputAutoComplete.setOnKeyPressed((event) -> {
            if(inputAutoComplete.getText().length() > 4){
                System.out.println(inputAutoComplete.getText());
                possibleWordSet = dbconn.getSuggestedData(inputAutoComplete.getText());
                System.out.println(possibleWordSet);
                TextFields.bindAutoCompletion(inputAutoComplete,possibleWordSet);
            }
        });
    }
}

Can someone give me some examples of how to implement this feature or just tell what am I doing wrong?

  • Use the correct version of ControlsFX (9). – fabian Dec 09 '18 at 18:27
  • I'm using `org.controlsfx:controlsfx:9.0.0` – Wojciech Miśta Dec 09 '18 at 18:39
  • 4
    I found a solution! I had to add `--add-exports=javafx.base/com.sun.javafx.event=ALL-UNNAMED` to the VM Options. – Wojciech Miśta Dec 09 '18 at 19:28
  • **Note:** The need for the `--add-exports` VM argument found by the OP and shown in the answers, along with other variations of this argument, is documented at https://github.com/controlsfx/controlsfx/wiki/Using-ControlsFX-with-JDK-9-and-above – Slaw Jul 07 '22 at 07:27

2 Answers2

6

If you are using ControlsFX 11, add the following VM option to your runtime command line:

--add-exports javafx.base/com.sun.javafx.event=org.controlsfx.controls

Note: Previous answers (on other sites and also here on SO) have suggested the following:

--add-exports javafx.base/com.sun.javafx.event=ALL-UNNAMED

However, recent versions of ControlFX appear to be modularized and the modules are no longer unnamed. If you are using a previous version (say, version 9), use the ALL-UNNAMED variant of the command line option.

Barry Brown
  • 20,233
  • 15
  • 69
  • 105
  • How does this look in `module-info.java`? – SedJ601 May 24 '22 at 20:08
  • @SedJ601 It would look like `exports com.sun.javafx.event;` or `exports com.sun.javafx.event to org.controlsfx.controls;`. But that would be in the module descriptor of `javafx.base`. You can't modify another module from your own module descriptor. That leaves using `--add-exports` as shown above. Or, to do this programatically, you could build your own `ModuleLayer` for JavaFX + ControlsFX (+ your own code) and make use of the `ModuleLayer.Controller` API. Or you could fork and build JavaFX yourself to add the desired `exports` directives. – Slaw Jul 07 '22 at 07:20
  • @Slaw, If I am using an IDE, let's say Netbeans, where do I do `--add-exports javafx.base/com.sun.javafx.event=org.controlsfx.controls`? I can't seem to edit `Run->VMOptions:` under `Project Properties`. I get a message that says `One of /Run/Debug/Profile actions has been modified and the Run panel cannot be safely edited`. – SedJ601 Jul 08 '22 at 02:04
  • I just noticed the answer by Wizards. I am going to try that. – SedJ601 Jul 08 '22 at 02:05
5

If you ended up here and are using the maven plugin (javafx:run) to run your app, you can use the following configuration to get it running.

        <plugin>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>0.0.4</version>
            <configuration>
                <mainClass>some.package.App</mainClass>
                 <options>
                    <option>--add-exports</option>
                    <option>javafx.base/com.sun.javafx.event=org.controlsfx.controls</option>
                 </options>
            </configuration>
        </plugin>
WizardsOfWor
  • 2,974
  • 29
  • 23