0

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ali97
  • 91
  • 8
  • Just for the record: you do **not** need that extra Runnable and invokeLater to **initially** create your GUI object. You only need to use invokeLater when the GUI elements are already showing up on the screen, and you want to manipulate them at that point! – GhostCat Jun 02 '19 at 10:04

1 Answers1

0

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!

GhostCat
  • 137,827
  • 25
  • 176
  • 248