1

In this programm i wanna switch between scenes by button click.I get an Exception on the line where i want to set the Location of the FXML Loader. In other Tutorials or posts i see it is done in the same way i did it.I also checked the Path to the fxml file a hundred times.

So the special case is to switch from QuestionTableViewController with the fxml of tableViewQuestion.fxml to the tableViewUser.fxml with the Controller UserTableViewController. At least i start the Programm in MyApplication.

Could that Exception arrive, because my Class is implementimg initializable? And if yes why and how to fix this? Or maybe because i have different Controllers for the fxml-files? but why and how could i fix this?

So now i ask you, where is my dumb fault at that Programm?

Controller with the switchMethod:

public class QuestionTableViewController implements Initializable{
    @FXML
        TableView<Question> questionTable;
    public void changeToUserDatabase() throws IOException {
         Scene quizScene = new Scene(FXMLLoader.load(getClass().
                           getResource("src/view/tableViewUser.fxml")));
        Stage primaryStage = (Stage) questionTable.getScene().getWindow();
        primaryStage.setScene(quizScene);
        primaryStage.show();
    }}

Start Method:

public class MyApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("view/tableViewQuestion.fxml"));
        primaryStage.setTitle("Database of Century");
        primaryStage.setScene(new Scene(root, 750, 500));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

Here you can see the structure of my Programm

PS: I just show the methods i think are needed, of course i had a button with the onMouseClick method and a content for my tableView. If you think they could be relevant please commit and i will add it in the question. :)

Pommeschica
  • 109
  • 12
  • 3
    a) `src` is the directory containing the source files. It's not part of the package structure. b) the path you use is relative to the class's package. You're looking for the resource at location `/controller/src/view/tableViewUser.fxml` You should use a proper absolute path instead: `/view/tableViewUser.fxml` – fabian Mar 02 '18 at 11:08
  • thanks for this, however it do also not work with just /view/tableViewUser.fxml – Pommeschica Mar 02 '18 at 11:23

1 Answers1

2

The problem is that you are trying to access the resource in the wrong place. Based on what I read in this question, you should go up one directory to access the fxml file correctly. Try this:

    Parent root = FXMLLoader.load(getClass().getResource("../view/tableViewQuestion.fxml"));

Where .. should indicate looking at the parent directory.

EDIT:

The above will not work inside a packaged jar. To easily overcome your issue, create a class in the views package and use it to load resources.

For example create class ViewLoader in src/view and load your fxml files as follows:

    Parent root = FXMLLoader.load(ViewLoader.class.getResource("tableViewQuestion.fxml"));
TM00
  • 1,330
  • 1
  • 14
  • 26
  • 1
    You can't use `..` in a resource name as it's not a valid Java identifier: see https://stackoverflow.com/questions/34755630/javafx-location-is-not-set-error. In particular, it won't work when the application is packaged as a jar file. – James_D Mar 02 '18 at 12:05
  • That makes sense. So I suppose the best thing to do is to create a class inside the package to load the resources from? – TM00 Mar 02 '18 at 12:10
  • 1
    That's what I usually do (actually, I typically package the FMXL and its controller together, so the controller class works for this purpose). The other way is just to use the absolute path. – James_D Mar 02 '18 at 12:12
  • I also typically place the controller and FXML together, its just easier to find things in my opinion. – TM00 Mar 02 '18 at 12:28