Определите GUI в файле fxml
и укажите его контроллер:
GUI2.f xml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.StackPane?>
<StackPane fx:id="root" prefHeight="150.0" prefWidth="200.0" xmlns="http://javafx.com/javafx/10.0.1"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="fx_tests.GUI2Controller" />
Выполните инициализацию в контроллере. Добавьте получатели для информации, которую вы хотите получить из контроллера позже:
import javafx.fxml.FXML;
import javafx.geometry.Point2D;
import javafx.scene.layout.StackPane;
public class GUI2Controller{
@FXML StackPane root;
private Point2D[] events;
private SimpleHeatMap heatMap;
@FXML
public void initialize() {
//initialize here all info that you want to retrieve later
heatMap = new SimpleHeatMap(400, 400, ColorMapping.LIME_YELLOW_RED, 40);
events = new Point2D[] {
new Point2D(110, 238),
new Point2D(120, 144),
new Point2D(207, 119),
new Point2D(315, 348),
new Point2D(264, 226),
new Point2D(280, 159),
new Point2D(240, 186),
new Point2D(228, 170),
new Point2D(234, 160),
new Point2D(214, 170),
new Point2D(200, 200),
};
//you could also populate root
//root.getChildren().setAll(heatMap.getHeatMapImage());
}
public SimpleHeatMap getHeatMap() {
return heatMap;
}
public Point2D[] getEvents() {
return events;
}
}
Используйте его в приложении:
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Point2D;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class FxmlTest extends Application {
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("GUI2.fxml"));
Pane root = fxmlLoader.load();
GUI2Controller controller = fxmlLoader.getController();
//retrieve info from controller
SimpleHeatMap heatMap = controller.getHeatMap();
Point2D[] events = controller.getEvents();
//todo: use as needed
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(null);
}
}