GUI
[Framework practical 6 of 7]
So, where do we put the code that used to create the io
and store
objects?
The place to put these in the Analyst class is the same place we put the data
array inside the Storage class: at the top of
the class, just after the class declaration and just before the first method. That way they are instance
variables that can be seen across the whole class. Make your io
and store
objects in
this location in the Analyst class now.
Stick a System.out.println("Hello World");
in the setData
method of
Storage so we can test that our menu code is working, compile the code and run it.
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.
Anyhow, 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 Analyst's constructor:
openMenuItem.addActionListener(this);
This code registers the Analyst class as the listener of the MenuItem (as this code is inside the Analyst class, the "this
" refers to
the Analyst 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..." the data as a file, using the code we wrote last week.
Once you've done that, show us you've got it working, and you're done.
If you want to experiment, try making yourself a "Process" menu that has an option on it for generating random data. You could also add FileDialogs to your code.