How to create an alias to an ArrayList object used in another class

306 views Asked by At

So I have 3 different classes, one: to create State objects, two: to read data from a csv file and pass this data to the State constructor in order to create an ArrayList of State objects, and three: to display this information in a GUI.

the code for two is here:

public class DataReader
{
   private ArrayList<State> states  = new ArrayList<State>();
   private File file = new File("data_11_09.csv"); 

public ArrayList<State> readData() throws FileNotFoundException {
       Scanner read = new Scanner(file);
       read.useDelimiter(",");
       
       String stateName;
       int infectionNum;
       int deathNum;
       
       while (read.hasNextLine()) {
           stateName = read.nextLine();
           infectionNum = read.nextInt();
           deathNum = read.nextInt();
           states.add(new State(stateName, infectionNum, deathNum));
    }
        read.close();
    
        return states;
    }
}

and then code for class three:

public class ChartWindow extends Application
{
    ArrayList<State> displayStates = new ArrayList<State>();
    final double RADIUS = 2000;
    int horSpace;
    int verSpace;
   
    @Override
    public void start(Stage primaryStage) {
        
   
    }
    
    public State drawState() {
        collections.copy(displayStates, states);
    }
}

however under the drawState() method im getting the error that the variable "states" can't be found. I'm not sure how to reference the states ArrayList from class two to class three. Any help would be appreciated...sorry if this was long winded but im new here

1

There are 1 answers

2
jewelsea On

You could try something like this:

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

public class ChartWindow extends Application {
    @Override
    public void start(Stage stage) {
        DataReader reader = new DataReader();

        ListView<State> stateListView = new ListView<>(
            FXCollections.observableArrayList(
                reader.readData()
            )
        );

        stage.setScene(new Scene(stateListView));
    }
}

Key points:

  1. You don't need to have a drawState() method, JavaFX will automatically draw things when they are in the scene and update them if you are updating observable properties of scene controls.
  2. The JavaFX application is the entry point for execution, so you need to have it create the things it needs to run (e.g. the application creates the DataReader, not the other way round).
  3. Because the JavaFX application is creating the data reader, it will automatically have a reference to it, so you don't need to pass a reference to the DataReader around.
  4. You could use a TableView rather than a ListView, I just used a ListView as it requires less code.
  5. For the ListView to display the items in a meaningful way, you can add a toString method to your State object (or implement a cell factory on the ListView).
  6. For your actual application you may have different requirements, I just provided the code above to get you started on a better track.

Some of the other comments are correct, to succeed in this endeavor you will likely need to spend significant time and effort to study some basic programming tutorials (e.g. "Trails Covering the Basics").