0

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • create a getter for the states in the DataReader class and pass the DataReader object to the ChartWindow class. Then you can call the getter on the DataReader object inside the ChartWindow class – RAZ_Muh_Taz May 05 '21 at 22:54
  • The code you posted does nothing. As it relates to the variable `states`, you first need to do something like `Type states = new Type(...)`. This is programming 101. – SedJ601 May 05 '21 at 23:20
  • 1
    My guess is that what you want to do something like `DataReader dataReader = new DataReader(); states = dataReader.readData()`. – SedJ601 May 05 '21 at 23:22

1 Answers1

2

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").

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • This code is explicitly doing file handling from inside the FXAT, which is not a good idea. You need to run the file access in a background thread, probably using Task. – DaveB May 06 '21 at 13:08
  • @DaveB I'd agree that is true for a robust, long-lived app that is processing large files, but for a relatively inexperienced developer who is having difficulties determining how to access object references within a very small application, I think the solution proposed here is likely reasonable. For others though, your comment on best practices for multi-threaded programming in JavaFX applications is relevant. – jewelsea May 06 '21 at 19:27