bikleDemo4.htm
05-06-2002 Bikle
-----------------
Bikle Java Demo #4
This file documents my efforts to complete Bikle Java Demo #4.
The goal of this demo is simple: build a bare-bones Swing GUI
around a three classes which act as memory structures for Strings.
wrapArray-java.txt
Most of this demo revolves around this class.
This is a class which is a simple wrapper for an array of type String.
Also it contains these methods which come in handy for the GUI:
addName()
delName()
findName()
wrapArray2-java.txt
This class is similar to wrapArray.java. I wrote wrapArray2.java
to make it more suitable for sharing data with a GUI.
mainWrapArray-java.txt
I used this file to get my mind connected to how to use the wrapArray
methods. wrapArray is aptly named; I see it as an array of Strings with
some useful methods.
Some key syntax in this file:
// create a new wrapArray
wrapArray AwrapArray = new wrapArray();
// Add a name
AwrapArray.addName("Dan");
Also this file helped me work around some spastic behavior within
JDev. JDev gets weird when dealing with 'the command line'. It's
important to have it get stdin/stdout from a 'console window' rather
than the 'message view'.
I fire up the 'console window' via:
project->project properties->run/debug->'send run output to console window'
charUI-java.txt
I see this file as a command-line-character interface. It makes
use of the fromKeyBoard class to ask for user input. It then passes
responses to methods within a wrapArray object.
In a way it mirrors proj1Gui.java since it too creates a 'wrapArray'
object which it then interacts with.
mainCharUI-java.txt
Not much to this file, it just creates a charUI object inside of a main method.
fromKeyBoard-java.txt
This is a class which helps me read input from the keyboard.
proj1Gui-java.txt
This is the key file for the GUI part of the demo. The class creates:
- GUI components
- A 'wrapArray2' object
One of the key things I learned from this effort was that the 'wrapArray2' object
needed to be created within what I call 'the GUI' class. I learned this from
Sean who learned it from the smart guy in the 3rd row.
Another thing which became obvious to me as I worked on this demo is
that the wrapArray class was not well suited for a GUI. It was sending
output to the user via the System.out.println () method. I copied
wrapArray to wrapArray2 and enhanced the copy so that methods which
needed to send output to the user returned a String (which I could then
send to any kind of UI which can handle a String).
The rest of the things I learned actually came from lecture notes but
this project hammered them home. Aside from demonstrating some simple
GUI objects this demo also shows how to implement an Action Listener.
Here is some syntax associated with the Action Listener:
Piece 1 (notice syntax: "implements ActionListener"):
class proj1Gui implements ActionListener {
Piece 2:
// register this JTextField with the action listener
cmdJTextField.addActionListener(this);
Piece 3:
public void actionPerformed(ActionEvent Event) // This declared by ActionListener interface
Piece 4:
// Use Event.getSource( ) to query the action listener.
// Notice we cast the return value to something we can push into an if {...}
JTextField activeField = (JTextField) Event.getSource( );
// if the user activated the cmdJTextField, we respond here, else we do nothing.
if (activeField == cmdJTextField)
{
// System.out.println("you chose cmdJTextField ");
// Grab a copy of the entered command and place the copy in confJTextField
String capturedCmd = cmdJTextField.getText();
confJTextField.setText(capturedCmd);
When I teach new concepts, I try to wrap them in a visual way.
I see the action listener as a little robot in my program. His only
job in life is to sit in a chair and wait for the user to press the Enter
key inside of cmdJTextField. Once the user does that, the robot gets
out of his chair and executes the instructions I put in the method
named: "actionPerformed()". Then, he sits back down in his chair.
For this demo, I had to fit together four pieces of code (displayed above)
to make it work.
mainProj1-java.txt
I've not put much in this file; the comments in it tell most of the story:
// The proj1Gui class constructs a simple interface, an array for
// strings, and some array manipulation methods.
proj1Gui aProj1Gui = new proj1Gui();
proj1WindowControl-java.txt
This class creates a 'window listener' which makes the X thing
shutdown the program when I click it. The window listener is like the
action listener; it sits around and waits for the user to take an
action (in this case: click the X thing in the upper right hand corner).