1

I'm trying to add a MenuBar to my program in javaFX, but it's giving me the following error:

Exception in Application constructor

Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.scene.control.ControlHelper (in unnamed module @0x5557cafa) 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 @0x5557cafa at fileHolder.TicTacToeMain.(TicTacToeMain.java:17) ... 13 more

I don't know how this at all, and nobody I know of is having the error. My code is below, the SDK is Java 11, and the javaFX version is also 11.

//GUI elements
Canvas canvas; //drawing canvas
MenuBar menuBar = new MenuBar();//This is where it breaks.
Menu resetMenu = new Menu("Reset...");
MenuItem resetBoard = new MenuItem("Reset Board");


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

@Override
public void start(Stage mainStage) {
    int canvasHeight = 750;
    int canvasWidth = 750;

    mainStage.setTitle("Offspring Tree Example"); //window title

    resetMenu.getItems().add(resetBoard);//Adds resetBoard menu to the resetMenu Item
    menuBar.getMenus().add(resetMenu);//Adds resetMenu to menuBar

    VBox root = new VBox(); //root node of scene graph
    Scene theScene = new Scene(root); //our GUI scene
    mainStage.setResizable(false); //don't allow window to resize
    mainStage.setScene(theScene); //add scene to our app's stage

    canvas = new Canvas(canvasWidth, canvasHeight); //GUI element we will draw on
    root.getChildren().add(canvas);
    root.getChildren().add(menuBar);

    mainStage.show();
}
  • Which components are those? Most Swing components start with a "J" (`JMenuBar`, etc.) but older AWT components don't. Which have you got there? (Show us the `imports`.) – markspace Mar 27 '20 at 02:29
  • @markspace Question is tagged [tag:javafx]. – Slaw Mar 27 '20 at 02:31
  • @Slaw But mistakes happen, so let's be sure we're all on the same page. Esp. with a weird error like this. – markspace Mar 27 '20 at 02:31
  • @markspace The error is caused by using the `javafx.controls` module from the classpath instead of the modulepath. It's actually a common error for people using JavaFX 11+ for the first time, if the amount of questions about this is any indication (and the duplicate should solve the issue). If the OP was importing AWT classes I suspect the code wouldn't even compile, let alone throw an `IllegalAccessError` (which explicitly mentions JavaFX code). – Slaw Mar 27 '20 at 02:34
  • @markspace That said, including the imports is always a good policy (otherwise the example isn't truly a [mre]). – Slaw Mar 27 '20 at 02:36

0 Answers0