1

enter code hereI want a Label to transition from not being shown(0.0) to being shown(1.0)

    @FXML
private Label welcomeLabel;

public FadeTransition ft = new FadeTransition(Duration.millis(3000));


public void init(){

    ft.setNode(welcomeLabel);

    ft.setFromValue(0.0);
    ft.setToValue(1.0);
    ft.setCycleCount(1);
    ft.setAutoReverse(false);

    ft.play();

}

Here is the app class

package com.ben.main;

public class App extends Application {

private Stage primaryStage;
private Scene loginScene;
LoginUIController loginUIController = new LoginUIController();

public void start(Stage primaryStage) {

    this.primaryStage = primaryStage;

    initApp();
    loginUIController.init();

}

private void initApp() {

    Parent root = null;

    try {
        root = FXMLLoader.load(getClass().getResource("loginUIFXML.fxml"));
    } catch (IOException e){
        System.err.println("There was an error... " + e);
    }

    loginScene = new Scene(root);

    primaryStage.setTitle("project");
    primaryStage.setResizable(false);
    primaryStage.setScene(loginScene);
    primaryStage.show();


}

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

}

Do I have to add it to the scene here as well? I'm having trouble and currently just typing stuff to get the edit updated.

Benton Justice
  • 111
  • 1
  • 2
  • 7

1 Answers1

1

The FXML loading process for controllers works by reflection. It will invoke a method named initialize(). It won't know anything about a method named init() and so would never call that. So you should change your method name from init() to initialize().

I call the init method after the FXML file is loaded.

Yes, that can now be seen with the additional code you have added to your question.

But, you call init on a new instance of a controller which is not created by the FXMLLoader, instead of invoking init on the controller created by the FXMLLoader. The instance of the controller which you created with new is not associated with the scene graph in any way.

So, don't do that, don't create a new controller with new. Instead, use the controller which is created by the loader.

If you want to get a reference to the controller which was loaded by the FXMLLoader, you should get it from the loader, as exemplified by the following answer:

The relevant parts of which are copy and pasted below:

FXMLLoader loader = new FXMLLoader(
  getClass().getResource(
    "customerDialog.fxml"
  )
);

Stage stage = new Stage(StageStyle.DECORATED);
stage.setScene(
  new Scene(
    (Pane) loader.load()
  )
);

CustomerDialogController controller = 
  loader.<CustomerDialogController>getController();
controller.initData(customer);

Just adapt the pattern above for your code.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406