У меня есть Main.java:
Код: Выделить всё
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
//Primary application loader
try {
DataModel dataModel = new DataModel();
BorderPane root = new BorderPane();
FXMLLoader uiLoader = new FXMLLoader(getClass().getResource("views/MainUI.fxml"));
root.setCenter(uiLoader.load());
UIController uiController = uiLoader.getController();
FXMLLoader counterLoader = new FXMLLoader(getClass().getResource("views/Tracker.fxml"));
root.setTop(counterLoader.load());
TrackerController trackerController = (TrackerController) counterLoader.getController();
uiController.initModel(dataModel);
trackerController.initModel(dataModel);
Scene scene = new Scene(root,400,400);
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);
}
}
Код: Выделить всё
package application;
import javafx.fxml.FXML;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
public class TrackerController {
private DataModel dataModel;
private Counter counter;
@FXML
private Label countLabel;
@FXML
public StringProperty labelTextProp = new SimpleStringProperty("Start clicking");
public void initModel(DataModel dataModel) {
if (this.dataModel != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.dataModel = dataModel;
counter = dataModel.getCounter();
// System.out.println(counter.getTotal());
counter.getProperty().addListener((observable, oldValue, newValue) -> {
labelTextProp.set("Click! " + newValue + " total clicks!");
System.out.println(labelTextProp.get());
});
//just a test
labelTextProp.addListener((observable, oldValue, newValue) -> {
System.out.println("Changed: " + oldValue + " -> " + newValue);
});
}
}
Код: Выделить всё
package application;
public class UIController {
public DataModel dataModel;
public void initialize() {
}
public void initModel(DataModel dataModel) {
if (this.dataModel != null) {
throw new IllegalStateException("Model can only be initialized once");
}
this.dataModel = dataModel;
}
public DataModel getDataModel() {
return this.dataModel;
}
}
Код: Выделить всё
Код: Выделить всё
package application;
public class ClickerController {
public DataModel dataModel;
public void initialize() {
}
// public void initModel(DataModel dataModel) {
// if (this.dataModel != null) {
// throw new IllegalStateException("Model can only be initialized once");
// }
// this.dataModel = dataModel;
// }
//This is what is getting run in the UI Controller
public void click() {
System.out.println("Click");
}
}
Подробнее здесь: https://stackoverflow.com/questions/789 ... hild-class