bngschmnd
bngschmnd

Reputation: 111

Edit class and method so its callable from another method

my Java is a bit rusty so I have a very basic issue.

I have a class Dashboard which eventually creates an JFXPanel chartFxPanel and I want chartFxPanel to display a specific scene using chartFxPanel.setScene(scene).

I have another class BarChart which will eventually generate a barchart from SQL Data.

For learning reasons I got this BarChart source code from oracle tutorials which generates a scene, but, unfortunately also has a main method which is more then I need right now.

updated code implementing Sabirs Answer

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class BarChartSample extends Application {
    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";

@Override public void start(Stage stage) {
    stage.setTitle("Bar Chart Sample");
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String,Number> bc = 
    new BarChart<String,Number>(xAxis,yAxis);
    bc.setTitle("Country Summary");
    xAxis.setLabel("Country");       
    yAxis.setLabel("Value");

    XYChart.Series series1 = new XYChart.Series();
    series1.setName("2003");       
    series1.getData().add(new XYChart.Data(austria, 25601.34));
    series1.getData().add(new XYChart.Data(brazil, 20148.82));
    series1.getData().add(new XYChart.Data(france, 10000));
    series1.getData().add(new XYChart.Data(italy, 35407.15));
    series1.getData().add(new XYChart.Data(usa, 12000));      

    XYChart.Series series2 = new XYChart.Series();
    series2.setName("2004");
    series2.getData().add(new XYChart.Data(austria, 57401.85));
    series2.getData().add(new XYChart.Data(brazil, 41941.19));
    series2.getData().add(new XYChart.Data(france, 45263.37));
    series2.getData().add(new XYChart.Data(italy, 117320.16));
    series2.getData().add(new XYChart.Data(usa, 14845.27));  

    XYChart.Series series3 = new XYChart.Series();
    series3.setName("2005");
    series3.getData().add(new XYChart.Data(austria, 45000.65));
    series3.getData().add(new XYChart.Data(brazil, 44835.76));
    series3.getData().add(new XYChart.Data(france, 18722.18));
    series3.getData().add(new XYChart.Data(italy, 17557.31));
    series3.getData().add(new XYChart.Data(usa, 92633.68));  

    Scene scene  = new Scene(bc,800,600);
    bc.getData().addAll(series1, series2, series3);
    setScene(scene);
    stage.setScene(scene);

    stage.show();
}

public void setScene(Scene scene) {
    this.scene = scene;
}

public static Scene getScene() {
    return scene;
}

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

How I want to access the scene in my Dashboard-Class:

    private void initFX(JFXPanel chartFxPanel) {
    Scene scene = BarChartSample.getScene();
    chartFxPanel.setScene(scene);
}

So what I would need now is a way/explanation how to turn this class into a class which returns scene and which i can call from inside my Dashboard class.

Thanks for your help,

b

Upvotes: 0

Views: 120

Answers (1)

Sabir Khan
Sabir Khan

Reputation: 10142

I have not understood your question fully so below is answer that might solve your issue as per my understanding. Let me know if I misunderstood your question as I am not so sure what to ask to further explain your question.

You can create an class instance variable and use getter & setter methods in class - BarChartSample. You can leave main()method as is since that is harmless.

So,

public class BarChartSample extends Application {
 .....
 .....
 //Add this 
 private Scene scene;

@Override 
public void start(Stage stage) {
.....
.....

Scene scene  = new Scene(bc,800,600);
bc.getData().addAll(series1, series2, series3);
stage.setScene(scene);
//Add this line 
setScene(scene);
stage.show();

}
//Add these two methods 

public void setScene(Scene scene){
 this.scene = scene;
}



 public Scene getScene(){
    return scene;
   }
  }

Now whichever class want to access this scene can access via getScene() method.

Upvotes: 1

Related Questions