CaptKirk
CaptKirk

Reputation: 25

Sending data from one tab to another tab in javafx 8 with eclipse and scene builder

I created two Tabs with Scene Builder, one fxml-file for each tab. These two files are included in an main.fxml file. In Eclipse I have one controller for each fxml file (MainController, Tab1Controller, Tab2Controller). To shorten these explanation. I want to send or load Strings one tab to the other tab via MainController (Like the mediator pattern). But i get the following error code

javafx.fxml.LoadException: 

/C:/Users/.../javafxmars/Test_TabController/bin/view/Main.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
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.Main.start(Main.java:14)
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)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 17 more
Caused by: java.lang.NullPointerException
at controller.MainController.initialize(MainController.java:14)
... 27 more

I searching for my mistake since a few days, but i cant find my anything. I looked for the ids a several times, but nothing. The FXML loader seems alright too.

public void start(Stage primaryStage) {
    try {
        new FXMLLoader();
        Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

The MainController looks as followed:

public class MainController {

@FXML Tab1Controller tab1controller;
@FXML Tab2Controller tab2controller;

@FXML public void initialize(){
    System.out.println("Program initilized");
    tab1controller.init(this);
    tab2controller.init(this);
}

public String getTextloadedfromTab1() { 
    return tab1controller.label1.getText();
}

public void setTab2LabelText(String text) {
    tab2controller.label2.setText(text);
}

}

If i have to I post the code from the two tabs or the code for the fxml files. I will be glad, if someone could help me out.

Here are the fxml documents

FXML Documents UPDATE

These project is based on these tutorial https://github.com/goranvasic/JavaFxTutorials/tree/master/JavaFxCommunicationBetweenControllers

Upvotes: 2

Views: 689

Answers (1)

DVarga
DVarga

Reputation: 21799

In your main FXML you include the tabs like:

<fx:include fx:id="tab_Test1" source="tab/Tab_Test1.fxml" />

And then in your MainController you inject it like:

@FXML Tab1Controller tab1controller;

Which is not the correct way to do it. The valid name is fx:idController (fx id with a "Controller" suffix):

@FXML Tab1Controller tab_Test1Controller;

Nested Controller naming.

Upvotes: 1

Related Questions