Passing information from an ArrayList from one class to a WindowsBuilder GUI class

35 Views Asked by At

I am to create an GUI that takes data I've read into an ArrayList, and perform actions on that data such as displaying, sorting and calculations.

My information is weather data.

In a class called "FileReading", I read my data from a csv into an ArrayList. I need to then pass this information to my JFrame GUI class named "WeatherGUI" and perform said actions on the data.

I am having trouble passing the information from my ArrayList over to my GUI class. As I've tested that it's reading data into the ArrayList fine, I won't include that code.

This is the relevant code I have in my WeatherGUI class and I'll describe the error below

public class WeatherGUI extends JFrame implements ActionListener {
    private ArrayList<Weather> weather;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    WeatherGUI frame = new WeatherGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public WeatherGUI(ArrayList<Weather> weather) {
        super("Weather");
        this.weather = weather;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 692, 561);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    }

The error I'm having is in the try statement where WeatherGUI wants a parameter relating to my ArrayList and I'm not sure what to put here. If I put in "weather" it tells me to make weather static which I know isn't correct. The code I've added is what the lecturer provided in a slide but I still get the error.

1

There are 1 best solutions below

0
GhostCat On

Here:

WeatherGUI frame = new WeatherGUI();

You designed your class in a way that the WeatherGUI needs a List (prefer to use List over ArrayList within your method signatures!) at creation time. Which makes sense.

But that means: you have to read that List object before you create the GUI object, like:

WeatherGUI frame = new WeatherGUI(FileReader.readWeatherInfos());

( where readWeatherInfos() would have a signature like public static List<Weather> readWeatherInfos() for example). Or slightly different, something like:

List<Weather> tempWeather = new FileReader().readWeatherInfos();
WeatherGUI frame = new WeatherGUI(tempWeather);

( here assuming that your read method isn't static )

You are correct about not making anything static in your class. The weather field of your class is fully correct. But you simply can't get to that field before the WeatherGUI object has been instantiated!