JavaFXML + Scenebuilder:LineChart不显示值

时间:2018-11-12 09:13:55

标签: java javafx fxml scenebuilder linechart

我有一个带有MenuItem的RootStage,并且想要将我的Rootstage的中心设置为一个AnchorPane,中间是一个LineChart

该图表在x轴上有一个CategoryAxis,在y轴上有一个NumberAxis

但是现在,如果调用带有Scene的{​​{1}}的Controller,我可以看到LineChart,但是它没有值。或至少不显示它们。

为什么LineChart没有填写?

或者为什么它不显示值?

MainController.class

LineChart

DailySceneController.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();
        }
    }

DailyScene.fxml

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);
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用控制器类扩展Application,希望可以为其调用生命周期方法。这是一种不好的做法,也不会导致生命周期方法的调用。

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

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