GUI
[Agent practical 7 of 9]


So, where do we put the code that used to create the io object?

The place to put this in the Model class is the same place we put the data array inside the Environment class: at the top of the class, just after the class declaration and just before the first method. That way it is an instance variable that can be seen across the whole class. Make your io object in this location in the Model class now.


Even though the code runs from a Window now, there's nothing to stop you still printing stuff to the command line with System.out.println to test code is working (ultimately, though, we don't want the user seeing any System.out.println stuff so we need to turn it off before we release the code).

Doomed: Stick a System.out.println("Hello World"); in the setData method of Environment, compile the code and run it.

You should find that nothing happens. This is because the MenuItem currently doesn't know anything about the Listener code: we haven't attached the two together. Add the following line to the end of the code making the menu in Model's buildGui():

   openMenuItem.addActionListener(this);

This code registers the Model class as the listener of the MenuItem (as this code is inside the Model class, the "this" refers to the Model class code). Compile and run the code, and you should now see the "Hello World" code running. You can then remove the System.out.println.


Now that all works, see if you can add in a new MenuItem to the File menu that allows you to "Save Results...", i.e. the data as a file, using the code we wrote last week. You can use exactly the same actionPerformed() method; it just needs another if-statement.

Then have a go at adding a second Menu ("Model") with a "Run" MenuItem that runs the model (note how handy it is that our runAgents() code is now separated off from the constructor where we originally wrote it). Again, you can use exactly the same actionPerformed() method.


Once you've done that, you're done.

If you want to experiment, try making yourself a "Process" menu that has an option on it for generating the default Enviromental data. You could also add FileDialogs to your code.