I'm trying to create a line chart using this tutorial example: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/line-chart.htm, which I've reworked slightly, but I'm not using the stage provided by the start
method of the Application
class. Rather, I'm trying to create multiple stages of my own. However, when I attempt to do so I just get presented with a blank white window which stops responding. This is the code:
public Chart() {
Stage stage = new Stage();
stage.setTitle("Test");
XYChart.Series series = new XYChart.Series();
series.setName("My portfolio");
series.getData().add(new XYChart.Data(1, 23));
series.getData().add(new XYChart.Data(2, 14));
series.getData().add(new XYChart.Data(3, 15));
series.getData().add(new XYChart.Data(4, 24));
series.getData().add(new XYChart.Data(5, 34));
series.getData().add(new XYChart.Data(6, 36));
series.getData().add(new XYChart.Data(7, 22));
series.getData().add(new XYChart.Data(8, 45));
series.getData().add(new XYChart.Data(9, 43));
series.getData().add(new XYChart.Data(10, 17));
series.getData().add(new XYChart.Data(11, 29));
series.getData().add(new XYChart.Data(12, 25));
NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
xAxis.setLabel("Time");
yAxis.setLabel("Value");
LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);
lineChart.setTitle("Test");
lineChart.getData().add(series);
StackPane root = new StackPane();
root.getChildren().add(lineChart);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
}
public Program extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
run();
}
private void run() {
Chart c1 = new Chart();
while (true) {
// Do something and update the charts.
}
}
}
I know that this isn't best practice of showing a stage, but I'm simply trying to get to grips with JavaFX. Can anyone see the problem?