J.Doe
J.Doe

Reputation: 21

Difficulty loading FXML file in Eclipse

Here is my class where I've written the one liner code to link the fxml file to the project I'm working on:

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;


public class towerOfHanoi extends Application implements Initializable 
{
    public static void main(String[] args) 
    {
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception 
    {       
        try
        {
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
        Scene s=new Scene(rootContainer);
        primaryStage.setScene(s);
        //primaryStage.setTitle("Towers Of Hanoi");
        primaryStage.show();
        }
        catch(IOException e)
        {               
        //          e.printStackTrace();
        }
}

@Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}

Any time I try to run this in eclipse, nothing happens. The square to terminate is red as if something is happening, but the UI I created in scenebuilder doesn't show.

****Update: This is the complete stack trace when I uncomment that piece of code:

Mar 14, 2017 4:01:49 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 8.0.102 by JavaFX runtime of version 8.0.101
javafx.fxml.LoadException: No controller specified.
/F:/2nd%20Year%20College%20Stuff/Semester%202/Event-driven%20Programming/2nd%20Year%20Workspace/CA%202/bin/application/userInterface.fxml:44

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.getControllerMethodHandle(FXMLLoader.java:557)
    at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:599)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.towerOfHanoi.start(towerOfHanoi.java:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)

Upvotes: 1

Views: 7189

Answers (1)

azro
azro

Reputation: 54148

As the error tells you : you need to associate a controller to the .fxml, like this :

FILE userInterface.fxml (you can have another container than a BorderPane but it will be the same, it has to be on the root parent)

<BorderPane fx:id="background" fx:controller="application.Controller">
  //Content ...
</BorderPane>

you can set the Controller into the fxml (see above) OR by SceneBuilder:

enter image description here


Also It will be clearer to separate the Launcher and the controller, like this :

FILE towerOfHanoi.java

public class towerOfHanoi extends Application{
    public static void main(String[] args){
        launch(args);
    }
    public void start(Stage primaryStage) throws Exception{       
        try{
            Parent rootContainer = FXMLLoader.load(getClass().getResource("/application/userInterface.fxml"));
            Scene s=new Scene(rootContainer);
            primaryStage.setScene(s);
            //primaryStage.setTitle("Towers Of Hanoi");
            primaryStage.show();
        }
        catch(IOException e){               
        //          e.printStackTrace();
        }
    }
}

FILE Controller.java

public class Controller implements Initializable {
    @Override
    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
    }
}

Upvotes: 4

Related Questions