How to use same method in two different classes (JavaFX with Scenebuilder)?

时间:2018-09-18 19:28:38

标签: java eclipse inheritance javafx scenebuilder

I am currently experimenting with JavaFX and SceneBuilder in eclipse to create and design my own program. In my first class "StartController" I am using a method called makeFadeIn. Basically, when I click a button another page loads up with a fade effect.

This is the code from StartController.java (notice makeFadeIn):

public class StartController {

@FXML
private AnchorPane rootPane;

private void makeFadeIn() {
    FadeTransition fadeTransition = new FadeTransition();
    fadeTransition.setDuration(Duration.millis(1000));
    fadeTransition.setNode(rootPane);
    fadeTransition.setFromValue(0);
    fadeTransition.setToValue(1);
    fadeTransition.play();
}

@FXML
private void loadSecondPage(ActionEvent event) throws IOException {
    AnchorPane startPage = FXMLLoader.load(getClass().getResource("SecondController.fxml"));
    rootPane.getChildren().setAll(startPage);
    makeFadeIn();
}

Next, my other class loads up called "SecondController.java". In this class, I'm using the exact same method makeFadeIn (but I had to write it twice since it didn't let me run the program).

This is the code from SecondController.java:

public class SecondController {

@FXML
private AnchorPane rootPane;

private void makeFadeIn() {
    FadeTransition fadeTransition = new FadeTransition();
    fadeTransition.setDuration(Duration.millis(1000));
    fadeTransition.setNode(rootPane);
    fadeTransition.setFromValue(0);
    fadeTransition.setToValue(1);
    fadeTransition.play();
}

@FXML
private void loadFirstPage(ActionEvent event) throws IOException {
    AnchorPane startPage = FXMLLoader.load(getClass().getResource("StartController.fxml"));
    rootPane.getChildren().setAll(startPage);
}

My question is: can I somehow call the makeFadeIn method from the first class so I don't have to write it in my second class? I guess I need to inherit it in some way but I'm not sure how. I tried declaring it public instead of private but that did not help much.

1 个答案:

答案 0 :(得分:2)

You could move this functionality to a base class:

public class BaseController {

    @FXML
    private AnchorPane rootPane;

    protected AnchorPane getRootPage() {
        return rootPane;
    }

    protected void makeFadeIn() {
        FadeTransition fadeTransition = new FadeTransition();
        fadeTransition.setDuration(Duration.millis(1000));
        fadeTransition.setNode(rootPane);
        fadeTransition.setFromValue(0);
        fadeTransition.setToValue(1);
        fadeTransition.play();
    }
}

And then have the other controllers extend it:

public class StartController extends BaseController {

    @FXML
    private void loadSecondPage(ActionEvent event) throws IOException {
        AnchorPane startPage = 
            FXMLLoader.load(getClass().getResource("SecondController.fxml"));
        getRootPane().getChildren().setAll(startPage);
        makeFadeIn();
    }
}

public class SecondController extends BaseController {

    @FXML
    private void loadFirstPage(ActionEvent event) throws IOException {
        AnchorPane startPage = 
            FXMLLoader.load(getClass().getResource("StartController.fxml"));
        getRootPane().getChildren().setAll(startPage);
    }
}