OO Programming in Java

Object Oriented GUI

GUIs can be made using Java's standard AWT classes and interfaces. Inner classes may cut down the amount of keystrokes required to make views but most of the organization and flexiblity is lost.

Think of views as a separate orthogonal object model. They usually require some thought. Remember to use the spiral development concept. Start simple and build.

Unless the GUI Component performs very simple tasks (e.g. a List that simply receives data and does not need to repsond to clicking or double-clicking) a new Class will be created for each GUI Component.

The OO GUI classes which will be demonstrated are simply another tool to help make view design and implementation simpler, more organized and easier to maintain.

Besides removing some of the dogwork of writing views these classes also help promote interwidget communication.

The SCView maintains a Hashtable of GUI components by name. If you had 5 buttons and 3 listboxes there would be 8 Hashtable entries instead of 8 instance variables (and their getters and setters cluttering the model).

Components are added to this Hashtable by saying:

// This is inside the main View class

this.addWidget("MyNewButton", new MyButton(this));

This places the instance of MyButton in the Hashtable.

To retrieve the MyButton instance simply say:

// This is inside the main View class

MyButton myButtonInstance = (MyButton) (this.getWidget("MyNewButton"));

See Exercise 3a Review - Zoo Example for the main example covered in class.

Below is an additional example

Object Model

Scenario Diagrams


AWT Example Code