I'm creating a simple javaFX program. In HelloApplication
I want to do the logic, and in HelloController
I want to listen to button clicks which then execute methods inside the HelloApplcation
.
HelloApplication.java:
Timeline timeline;
@FXML
HelloController HC;
FXMLLoader fxmlLoader;
private int counter=0;
public void start(Stage stage) throws IOException {
fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("hello-view.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 640, 480);
stage.setTitle("Program!");
stage.setScene(scene);
stage.show();
StartCounter("start");
}
public void StartCounter(String status)
{
HC = fxmlLoader.getController();
if(timeline==null) {
timeline = new Timeline(new KeyFrame(Duration.seconds(1), ev -> {
HC.getCmd().setText(String.valueOf(counter));
counter++;
}));
timeline.setCycleCount(Animation.INDEFINITE);
}else{
if(status=="stop")
timeline.stop();
else if(status=="start")
timeline.play();
}
}
HelloController.java:
HelloApplication helloApp=new HelloApplication(); //this creates another instance...
@FXML
protected void onHelloButtonClick() {
helloApp.StartCounter("stop");
}
But how can I access HelloApplication object that was created on launch()
?
Something like loader.getApplication()
would be perfect instead of creating a new object in controller (which doesn't work anyway).
Right now, after mouse click the helloApp.StartCounter() causes: java.lang.reflect.InvocationTargetException