首页 > 解决方案 > JavaFXML + Scenebuilder:LineChart 未显示值

问题描述

我有一个带有一些MenuItems 的 RootStage 并且想将我的 Rootstage 的中心设置为AnchorPaneaLineChart在中间。

图表CategoryAxis在 x 轴上有 a NumberAxis,在 y 轴上有 a。

Scene但是现在如果我用它调用控制器LineChart,我可以看到LineChart但它没有值。或者至少不显示它们。

为什么LineChart没有被填满?

或者为什么它不显示值?

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>

标签: javajavafxfxmlscenebuilderlinechart

解决方案


您正在Application使用您的控制器类进行扩展,期望它的生命周期方法被调用。这是不好的做法,也不会导致生命周期方法的调用。

如果要调用方法,请使用Initializable.initialize或仅使用无参数initialize方法:

@FXML
private void initialize() {
    this.setChartDaily();
}

推荐阅读