Java Article 8 - Windowed Applications part 3

Today we cover part 3 of our Java Windowed Application article. In part 2 we left of towards the end of the constructor method. You may want to open Part 1 of this article in another browser window so you can flip back and forth between the code there and the discussion here.

Take a look at this bad boy:

addWindowListener(

         new WindowAdapter()
	{
            public void windowClosing(WindowEvent e)
            {
               System.exit(0);
            }
        }
      );

OK... addWindowListener() is another type of constructor. It may be unusual looking for people who program in other languages. It is a method call contained within a method, contained within a constructor, contained within a method call, all contained within the constructor method.

Thank you Java, you just confused a bunch of people. Another way of looking at it is addWindowListener is being called with the constructor (new) and the windowClosing method as parameters.

Really all you need to know for now is this ties the standard windows close button (the "x" in the upper right corner of the window) to the method that closes the window and terminates your program, System.exit(0);

Without this you will have to hit your computer with a hammer until it terminates your program. Well you don't really have to hit your computer with a hammer, you can also use the red "terminate" button in Eclipse's console panel. There should be something similar in whatever editor you use.

So now we are done with the constructor. Remember the constructor method is used to create (instantiate) objects from our class. It contains all the instructions on how set up our object.

The next method is main. Main is the first thing run (the first method called) when our program is executed. Main creates an object (also called an instance) from our class with its constructor:

SimpleForm simpleFormObject = new SimpleForm();

The syntax is the same the same as the other constructors we talked about in part 2. We call our object "simpleFormObject." We can call it just about any thing we want, but its good to call it something you and other people will recognize, and its good to follow the camel case convention for object names. You could call it "x" but x does not tell us much about the object. When people see "simpleFormObject" hopefully they will think "you know… I bet this is an object of the class SimpleForm." Coming up with good names for variables and objects makes your code easier to read. It is an art more so than a science.

Now that we have created our object, simpleFormObject, we tell it to do some stuff:

simpleFormObject.setBounds(100, 100, 300, 300);
simpleFormObject.setTitle("SimpleForm");
simpleFormObject.setVisible(true);

The syntax used here is:

nameOfObjectRecivingTheMethodCall.methodNameCalled (arguments);

Put another way we are telling simpleFormObject to set its bounds with setBounds, to set its title window with setTitle and to appear as visible with setVisible(). The period (".") is called a delimiter. It links the addressee (the object we send the message to) to the message.

In English you could write this as:

bob.makeMeASandwich(mustard);

Here you are telling bob to execute his make me a sandwich method and you send the argument, mustard, which just happens to mean to add mustard to the sandwich.

If you look at our SimpleForm class you will notice you can not see these methods anywhere. These methods are inherited from the Frame class, since our class definition includes "extends Frame." Remember this is how we tell our SimpleForm class that it is a subclass of Frame, and it can use all of the pre-written methods Frame has.

Another way to think of this is when you see "extends", think "is a". SimpleForm is a Frame. Much like you are an individual, but you are also a member of a family. SimpleForm has all the methods and properties of a frame, and then you added a constructor and a couple of methods so SimpleForm is now a frame, and more! All the details of this relationship, and the way class structures can be used to build powerful, easy to code, and flexible programs goes a lot deeper than this, but you now have a good grasp of the basics.

Lastly we create the actionPerformed method:

public void actionPerformed(ActionEvent e)
{
   myTextField.setText("You just clicked the button");
}

This method gets activated (called) when we click the button. This is because we linked the button to this method with "myButton.addActionListener(this);"

When the button is clicked all the statements in this method get executed. Here, to keep it simple we just tell myTextField to set its text to what is shown in the quotes.

Compile and run this. This is a pretty simple program huh? You click the button and some text appears in the textfield. You also created an object. Your object is the window, whcich contains two other objects you instantiated, the button and the the textfield.

You also gave your object some behavior (the actionPerformed() method). If this is the first object you have ever created, congratulations!

If you have the time experiment with some of the arguments. Change the window size or change some of the text. Experimenting is one of the main ways a programmer learns new things.

That is it for article 8. Programming a windowed application in Java is not trivial to new programmers. In the next article we will take what we learned here and add to it, making our object do some thinking and decision-making.