Java Article 8 - Windowed Applications part 2

Welcome to part 2 of our Java Windowed Application article. In part 1 we left off at the class declaration, and you were cruelly exposed to protocols and subclassing.

Wikipedia.org has a ton of articles on programming concepts, when you encounter something interesting or just want to learn more you may want to read what the Wikipedia has to say about them. They can go into a lot more detail than I do, however the information here may be more specific to Java.

Don't feel bad if you don't understand everything. There is a tremendous amount of jargon in programming and the only way to start learning it all is to dive in over your head and start absorbing it. A first you will read a lot of things, whole paragraphs even, that you don't understand. Just start learning the individual words and eventually things will become clearer.

You may want to open Part 1of this article in another browser window so you can flip back and forth between the code there and the discussion here.

So we pick up right after the class declaration. The first thing we do is create some now objects. This is called "instantiating" objects. Instantiating means "create an instance of."

Button myButton = new Button("Click Me");
TextField myTextField = new TextField(20);
Panel myPanel = new Panel();

These are constructors. They instantiate objects for us. Here is the break-down of the syntax:

Button myButton…

In this part of the constructor you are telling the computer to set aside some of its memory and name this memory location myButton. Furthermore the memory location must be big enough to hold all the information that goes a long with this object, holding things like its name and location. This is why you have to declare the type (Button) so the computer knows what to expect here. This is just like declaring an int by using:

int numberOfZombies;

Now look at the second half of the statement:

= new Button("Click Me");

This tells the computer to create a new instance of the button class. That is what the new keyword does. The word Button here is a constructor method contained within the Button class. This Button constructor method automatically sets up a new button and you just have to pass it the button's caption (the button's visible label) as an argument ("Click Me").

So this constructor just tells the computer to create a new Button object, named myButton, and set its caption to Click Me. This is a powerful feature of OOP. One little constructor can create an object of incredible complexity from Java's pre-written classes, or a class written by you or someone else. You could create as many buttons here as you want to, you just have to give them unique names.

Anyway this is a just a standard button like you see in a simple Windowed program. Later we link the button to a method that will get executed when the button is clicked.

The next constructor creates a TextField which is just a little area (an interface object or element) that can display text, or where you can type text in. Again you have seen this before. The number in parenthesis here is the width (left to right) of the TextField. You want to make your TextField big enough to show your text and you want it to look good.

The units for this number are in columns. I don't remember learning about these units in grade school along with inches and millimeters do you?

The Java API defines a column as "an approximate average character width that is platform-dependent." This just means that the particular operating system (OS) will figure this out for you. It may be longer or shorter based on the OS and how the OS is configured / what its font settings are.

Next we make a Panel. A Panel holds other interface elements, in this case the button and the textfield. We use the panel to set the appearance of where and how their objects appear in the window. The panel itself is invisible.

Ok now for more OOP magic:

public SimpleForm()
{

}

This is a constructor method, Not to be confused with the constructor statements above. When our class, SimpleForm, is instantiated (when we ask it to create an object or an instance of itself) this method gets executed. This method sets up the new SimpleForm object the way we want it. This only gets run if we use a constructor statement to create a SimpleForm object, which we do later. You can recognize a constructor method because it has the same name as its class.

So as part of this set-up we tell our panel object, myPanel, to add to itself the textfield object and the button object as seen below:

myPanel.add(myTextField);
myPanel.add(myButton);

This is a break down of the syntax:

panelName.add(objectToBeAdded)

Not too complicated right? The period between myPanel and add is just of way of saying "tell myPanel to use the add method and send the add method the name of the object to add as an argument."

myPanel has its own methods and you can call them when you want to. They are otherwise invisible to you and you typically never see them, and they don't clutter up your program. They are all "under the hood." This is like stepping on the accelerator in your car: You don't have to know how the engine works or see the engine, you just know if you push down on the pedal the car will try to go faster.

Our class SimpleForm also has an add method. It got it (inherited it) from being declared a subclass of Frame. See a bit of the OOP power? Our class has a bunch of methods we did not have to write, all just by including "extends Frame" in the class declaration.

So next we use our classes add method to add the panel (which contains the button and textfield) to a layout manager. A layout manager is class that helps us organize the objects (components) in a window. There are different types layout managers and don't worry too much about them for now as they can be pretty confusing. This just tells BorderLayout to take myPanel and put it towards the top (north) of the window.

add(myPanel, BorderLayout.NORTH);

You can experiment by using other constants instead of .NORTH . After you get this running try .SOUTH .EAST .WEST and .CENTER and look at the effects.

myButton.addActionListener(this);

Here we tell our Button object, myButton, to add an action listener for itself. So now when we click the button the actionPerformed(ActionEvent e) method at the end of this program gets called. The action listener just runs invisibly in the background listens for someone to push the button.

We just have one statement left in our constructor and it is a crazy one. It is so crazy I am going to save it for part 3.

Get up out of your chair and move around some. You may want to soak your head in a bucket of cold water or perhaps Vodka. Seriously though, holding a cool washcloth over your closed eyes, for even just a few seconds, can do wonders to sooth tired eyes and make you feel refreshed.

Tune in next week for part three.