0

I'm trying to work on a LineChart in JavaFX , I have two classes the Main class and the Controller class , the Main class has the Stage and the Scene , and the Controller Class has the functionality of the LineChart , after initializing the LineChart in the Contoller Class and after the running the code , the LineChart is not displaying the coordinates on the chart , why is that happeing ?

Here's the Main Class :

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

import java.io.IOException;

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
    Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
    primaryStage.setTitle("Line Chart");
    primaryStage.setScene(new Scene(root, 900, 500));
    primaryStage.show();
}

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

Controller Class :

package sample;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

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

public class Controller implements Initializable {

@FXML
private ComboBox functionChooser;

@FXML
private TextField tfWidth,tfMin,tfMax;

@FXML
private RadioButton showFunction,hideFunction;

@FXML
private Label labelWidth,labelMin,labelMax;

@FXML
private LineChart lnChart;

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {

    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();

    String funcs[] = {"y(x) = sin(X)","y(x) = cos(x) - 2 * sin(x)","y(x) = sin(x*x)"};
    functionChooser.getItems().setAll(funcs);
    functionChooser.getSelectionModel().selectFirst();

    Stage stage = new Stage();

    lnChart = new LineChart<Number,Number>(xAxis,yAxis);
    XYChart.Series series1 = new XYChart.Series();
    series1.setName("Portfolio 1");
    series1.getData().add(new XYChart.Data(0, 23));
    series1.getData().add(new XYChart.Data(1, 14));
    series1.getData().add(new XYChart.Data(2, 15));
    series1.getData().add(new XYChart.Data(3, 24));
    series1.getData().add(new XYChart.Data(4, 34));

    lnChart.getData().add(series1);

}
}
  • again: [mcve] please .. mind the __M__ (visual sugar is unrelated to the problem .. ;) That said: _how can I add the LineChart which is in the Contoller class to the Scene which is in the Main class _ that's not possible, a node must not be shared across parents. Sounds like a xy-problem, though: what do exactly do you want to achieve? Make certain to understand how to pass parameters around (like f.i. the data that is shown in the chart), see https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – kleopatra Oct 03 '21 at 11:00
  • @kleopatra my problem is basically after adding the coordinates of x and y in the contoller class and after running the code , nothing is being displayed – Mohammed Hamdoon Oct 03 '21 at 11:14
  • 1
    @MohammedHamdoon Please, when you edit the question, don't remove relevant data. With a minimal example, you don't need all of the fields which were in the FXML file (and also in the controller), but you do still need a minimal FXML file required and controller to reproduce the issue (e.g. with just the line chart field), that is what kleopatra meant by M. – jewelsea Oct 04 '21 at 17:31
  • Creating a new Stage in the controller initialize method and never using it is a bit strange, it is not your issue here, as, unless you show the new stage it won't actually do anything, it is just a strange thing to do. – jewelsea Oct 04 '21 at 17:32

1 Answers1

0

Never set a member marked @FXML to a new value.

The FXML annotated members are created by the FXMLLoader and automatically added to the node hierarchy the loader creates.

What you are doing is creating a second line chart, putting data in it, and never adding it to the scene.

What you want to do instead, is put your data in the existing line chart that was already created by the loader.

jewelsea
  • 150,031
  • 14
  • 366
  • 406