Search code examples
javafxfadetransition

JavaFX FadeTransition in after init is finished via controler


I have this Application:

public class FOO extends Application {

    private Scene scene;

    @Override
    public void start(Stage stage) throws Exception {
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void init() throws Exception {

        URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
        System.out.println(resource);
        Parent root = FXMLLoader.load(resource);

        scene = new Scene(root);
    }

}

And this as a controler:

public class FXMLTabPaneController implements Initializable {

@FXML
private TabPane tabPane;

@FXML
private AnchorPane anchorPane;

@Override
public void initialize(URL url, ResourceBundle rb) {
    FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
    fadein.setFromValue(0);
    fadein.setToValue(1);
    fadein.play();

}

}

I wanted to show the Tabpane slowly after app start but it start with app Tabpane already seen.


Solution

  • Try this:

    public class FXMLTabPaneController {
    
        @FXML
        private TabPane tabPane;
    
        @FXML
        private AnchorPane anchorPane;
    
        @FXML
        private void initialize() {
    
        }
    
        public void show(WindowEvent event) {
            FadeTransition fadein = new FadeTransition(Duration.seconds(5), tabPane);
            fadein.setFromValue(0);
            fadein.setToValue(1);
            fadein.play();
        }
    }
    

    and FOO class

    public class FOO extends Application {
    
        private Scene scene;
        private FXMLTabPaneController controller;
    
        @Override
        public void init() throws Exception {
            super.init();
            URL resource = FXMLTabPaneController.class.getResource("FXMLTabPane.fxml");
            System.out.println(resource);
    
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(resource);
    
            Parent root = loader.load();
            controller = loader.getController();
    
            scene = new Scene(root);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            stage.setOnShown(controller::show);
    
            stage.initStyle(StageStyle.UNDECORATED);
            stage.setScene(scene);
            stage.show();
        }
    
        public static void main(String[] args) {
            Application.launch(args);
        }
    }