1

I am trying to display a JavaFX multi-line chart. I try to open this graph on a Jbutton click from another Jframe and pass a data array to the graph class.

How can I make this class into an object and have it show the graph when the Jbutton is clicked from another Jfame?

This is my edited code and two arrays showing temporary data.

import javafx.application.Application;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;


public class LineGrph extends Application {

@Override
public void start(Stage stage) {
    double LE[] = {23.5,12.0,44.0,12.6,8.5,4.6,53.6}; // this two arrays should pass from another Jframe
    double RE[] = {22.5,32.0,40.0,32.6,43.5,23.6,11.6};
    stage.setTitle("Line Chart Sample");
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
     xAxis.setLabel("Hz");
     yAxis.setLabel("DB");
     xAxis.setSide(Side.TOP);

    final LineChart<String,Number> lineChart = 
            new LineChart<String,Number>(xAxis,yAxis);

    lineChart.setTitle("Stock Monitoring, 2010");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("Portfolio 1");
    int count = 125;
    for (int i = 0; i<= 6 ; i++)
    {

    String x = Integer.toString(count);
    series1.getData().add(new XYChart.Data(x, LE[i]));
    count = count * 2;
    }

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("Portfolio 2");

    int count2 = 125;
    for (int p = 0; p<= 6 ; p++)
    {
    String x = Integer.toString(count2);
    series2.getData().add(new XYChart.Data(x, RE[p]));
    count2 = count2 * 2;
    }

    Scene scene  = new Scene(lineChart,800,600);       
    lineChart.getData().addAll(series1, series2);

    stage.setScene(scene);
    stage.show();


}


public static void main(String[] args) {
    Application.launch(args);
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Your code is an entire, standalone, application. The only way you can execute this code is by executing this application. If you want some of this code to be reusable in other applications (which is what I think you are asking), you need to create a new class (one that is not a subclass of `Application`) and move the reusable code there. See, perhaps, https://stackoverflow.com/questions/32464698/java-how-do-i-start-a-standalone-application-from-the-current-one-when-both-are – James_D Sep 27 '17 at 13:39
  • why don't you use a DialogPane class? Or, I've used reflection class with swing which is known as class loading of java, but i don't know much about javafx. but it is possible to invoke it by using the reflection api. – tommybee Sep 27 '17 at 14:43

0 Answers0