0

I've got a RootStage with some MenuItems and want to set the center of my Rootstage to an AnchorPane with a LineChart in the middle.

The chart got a CategoryAxis on the x-Axis and a NumberAxis on the y-Axis.

But now if I call the Controller for the Scene with the LineChart on it, I can see the LineChart but it has no values. Or at least doesn't show them.

Why is the LineChart not getting filled?

Or why doesn't it show the values?

MainController.class

public class MainController extends Application {

    private Stage primaryStage;
    private BorderPane rootLayout;

    @FXML
    private MenuBar menuBar;

    @FXML
    private Menu menuAnsicht, menuOptionen;

    @FXML
    private MenuItem menuItemTag, menuItemWoche, menuItemAbout, menuItemCloseWindow;

    @Override
    public void init() {
    }

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;

        initRootLayout();
    }

    private void initRootLayout() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/RootLayout.fxml"));
            rootLayout = (BorderPane) loader.load();
            Scene scene = new Scene(rootLayout);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch (Exception e) {
            logger.debug("ERROR: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @FXML
    public void handleTagItem(ActionEvent event) {
        try {
            BorderPane nowLayout = (BorderPane) menuBar.getScene().getRoot();
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("/de/mgo/temperaturstatistics/view/DailyScene.fxml"));
            nowLayout.setCenter(loader.load());
        } catch (Exception e) {
            logger.debug("ERROR: " + e.getMessage());
            e.printStackTrace();
        }
    }

DailySceneController.class

public class DailySceneController extends Application {

    @FXML
    private Label titleDaily;

    @FXML
    private LineChart<String, Number> dailyChart;

    @FXML
    private CategoryAxis xDaily;

    @FXML
    private NumberAxis yDaily;

    public int count = 0;

    public DailySceneController() {
        count++;
        System.out.println(count);
    }

    @Override
    public void init() {
        this.setChartDaily();
    }

    @Override
    public void start(Stage primaryStage) {

    }

    public void setChartDaily() {

        LocalDate localDate = LocalDate.now().minusDays(0);

        List<Temperaturen> listeNodemcu_JHL = MainController.getListeNodemcu_JHL();

        this.xDaily.setAutoRanging(false);
        this.yDaily.setAutoRanging(false);

        this.dailyChart.setAnimated(false);

        XYChart.Series<String, Number> seriesJHL = new XYChart.Series<String, Number>();
        seriesJHL.setName("Nodemcu_JHL");

        seriesJHL.getData().add(new XYChart.Data<String, Number>("Test", 15));
        this.dailyChart.getData().add(seriesJHL);
    }
}

DailyScene.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?package de.mgo.temperaturstatistics.controller.Controller?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.LineChart?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>


<BorderPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.mgo.temperaturstatistics.view.DailySceneController">
   <top>
      <Label fx:id="titleDaily" text="Temperatur-Statistik Tagesansicht" BorderPane.alignment="CENTER">
         <font>
            <Font name="System Bold" size="18.0" />
         </font>
      </Label>
   </top>
   <center>
      <LineChart fx:id="dailyChart" prefHeight="300.0" prefWidth="500.0" BorderPane.alignment="CENTER">
        <xAxis>
          <CategoryAxis fx:id="xDaily" animated="false" label="Uhrzeit" side="BOTTOM" />
        </xAxis>
        <yAxis>
          <NumberAxis fx:id="yDaily" animated="false" autoRanging="false" label="Temperatur" lowerBound="10.0" side="LEFT" tickLabelGap="1.0" tickUnit="1.0" upperBound="30.0" />
        </yAxis>
      </LineChart>
   </center>
</BorderPane>

1 Answers1

0

You're extending Application with your controller class an expect it's lifecycle methods to be invoked for it. This is bad practice and also does not result in the invocation of the lifecycle methods.

If you want a method to be invoked, use Initializable.initialize or simply a parameterless initialize method:

@FXML
private void initialize() {
    this.setChartDaily();
}
fabian
  • 80,457
  • 12
  • 86
  • 114
  • Could you explain what a lifecycle method is? And my problem doesn't seem to be fixed with that. The chart has still no values showing. – user10639546 Nov 13 '18 at 07:22