1

I am trying to add a label to a JavaFX application, however, whenever I try to make a Label, I get an error. Without the Label label = new Label("Label"); line I get no errors, and the program works as intended. Is there anything I can do to fix this?

I just started using JavaFX so I don't really know much.

import javafx.application.Application;
import javafx.scene.layout.FlowPane;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.Label;

public class Main extends Application {

public static void main(String[] args){
    launch(args);
}

public void start(Stage stage){
    stage.setTitle("Label");

    FlowPane rootNode = new FlowPane();

    Scene scene = new Scene(rootNode, 300, 300);

    stage.setScene(scene);

    //Whenever I add this line to the code I start getting errors.
    Label label = new Label("This is a JavFX Application");
    //rootNode.getChildren().add(label);

    stage.show();
}
}

Errors:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x406f1e78) cannot access class com.sun.javafx.scene.layout.RegionHelper (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.scene.layout to unnamed module @0x406f1e78
at java.base/java.lang.ClassLoader.defineClass1(Native Method)
at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1016)
at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:802)
at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:700)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:623)
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javafx.scene.control.Control.<clinit>(Control.java:86)
at Main.start(Main.java:23)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
Exception running application Main
SAIFUL HAQUE
  • 37
  • 1
  • 4
  • See https://stackoverflow.com/questions/53822178/i-am-now-using-javafx-in-the-java-jdk-11-and-getting-an-error-thrown-out-when-ex. Do the comments answer your question? – Code-Apprentice Jan 25 '19 at 22:31
  • If this is not a duplicate, please [edit] your question to include a [mcve] that shows your revised approach. – trashgod Jan 25 '19 at 23:08
  • @trashgod I didn't know there was another question with the same error. Thanks – SAIFUL HAQUE Jan 25 '19 at 23:23

1 Answers1

1

There's nothing wrong with your code! As you said you are starting now, I suppose you should have installed the latest version of Java: version 11.

What happens is that until version 10 of Java, if I'm not mistaken, JavaFX was included inside the JDK. But with the changes Oracle has made in its release policy, among other reasons, JavaFX is made available separately. Then you must install or enable JavaFX on your computer.

Here you can see how to install OpenFX and how to enable it in your project: OpenFX

I hope this can help you! Hugs!

Raffa
  • 58
  • 7