1 year ago

#332992

test-img

kyleryan1291

Bars not showing in bar chart JavaFX

I'm trying to implement a bar chart in JavaFX, but for some reason the data (bars of the chart) is not displaying when I run the application. The chart is set using fxml but the data is set with Java code. When the application runs all I see is the bare chart. How cna I get the bars to show in the chart?

Code:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;

public class HelloApplication extends Application {
    @Override
    public void start(Stage stage) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("HelloView.fxml"));
        Scene scene = new Scene(fxmlLoader.load());
        stage.setTitle("Hello!");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}

Data class:

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.ResourceBundle;

public class HelloController implements Initializable {
    final static String austria = "Austria";
    final static String brazil = "Brazil";
    final static String france = "France";
    final static String italy = "Italy";
    final static String usa = "USA";

    @FXML
    private AnchorPane anchorPane;

    @FXML
    private CategoryAxis countries;

    @FXML
    private NumberAxis values;

    @FXML
    private BarChart<String, Integer> barChart;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        XYChart.Series<String, Integer> series1 = new XYChart.Series<>();
        XYChart.Series<String, Integer> series2 = new XYChart.Series<>();

        countries.setLabel("Countries");
        values.setLabel("Value");

        series1.getData().add(new XYChart.Data<>("Austria", 50984));
        series1.getData().add(new XYChart.Data<>("Germany", 50984));
        series1.getData().add(new XYChart.Data<>("United Kingdom", 50984));
        series1.getData().add(new XYChart.Data<>("Greece", 50984));
        series1.getData().add(new XYChart.Data<>("France", 50984));

        series2.getData().add(new XYChart.Data<>("Austria", 50984));
        series2.getData().add(new XYChart.Data<>("Germany", 50984));
        series2.getData().add(new XYChart.Data<>("United Kingdom", 50984));
        series2.getData().add(new XYChart.Data<>("Greece", 50984));
        series2.getData().add(new XYChart.Data<>("France", 50984));

        barChart.getData().addAll(series1, series2);

        System.out.println("Inside initialize");
    }
}

This is the output: output

EDIT I figured out the answer: I never added fx:controller tag pointing to Java controller class to my fxml file.

java

javafx

fxml

0 Answers

Your Answer

Accepted video resources