Java Reference
In-Depth Information
as efficient as applications that use AWT. Because of these issues, a third GUI set was developed called the Standard
Widget Toolkit (SWT). SWT makes use of the native operating system components as much as possible, and offers
all of Swing's bells and whistles. SWT is open source (i.e., free) but it is not included in the JRE. When creating a GUI
using RAD, the SWT components are always offered as an option. When you choose a SWT component, RAD makes
the SWT library available to the project. The library has many jar files that contain all the SWT classes.
In general, all the components work similarly but they often do have different options, commands, and/or
syntax to perform the same functions. As mentioned, the Swing and SWT components often have extra features and
an added level of complexity that make them just a little harder to work with. Because of this, the examples in this
chapter will use the AWT components and in later chapters, we will show some Swing examples.
Composition
One way to look at a class/object is that they are composed of many other classes/objects. To summarize the first chapter:
Classes are made up of class variables and methods. Methods contain method variables and executable
statements. Variables (class or method) are defined as a class type and associated with an object of that class type.
For example, when an Employee is created a String object and a String variable named empName are created.
Then the String object is assigned to the variable. Therefore, the String object assigned to empName is one
component of the Employee object.
Objects, like Employee, are composed of many different objects. These objects have unique methods and
information that can be utilized by Employee. Therefore, it is said that a class like Employee acquires the functionality
of other classes through composition .
This explanation of composition may seem overly long (and painful) but it is necessary because the capabilities
of frames and labels can be accessed in a different manner called specialization . Later in the chapter, we will explain
specialization and contrast it with composition.
We want to create some GUI objects in the Employee class so that the employee name looks “pretty” when
displayed. We will do this by defining a frame and label in the Employee class. In other words, just as we created
a String object to hold the employee name, we want to create a Frame object and a Label object to display the
employee name.
Visual Components
A label is used to display text and labels are often used in tandem with entry fields. For example, when entering a
phone number on a Web page, there is an entry field to hold the information and a label (usually to the left of the
entry field) with a short description of the data to be entered (e.g., the text “Phone:”).
Labels must be assigned to a container . So, to create and use a label you need a container. Frame , Dialog , and
Panel are all examples of container classes. Our container of choice will be a Frame .
Label and Frame objects can be created just like String objects - create an instance of the appropriate class and
assign it to a variable. Said another way, create an object of the class type and assign it to a variable. If the explanation
is unfamiliar, the syntax should not be:
Label empNameLbl = new Label();
Just like a String , text can be assigned when the Label is defined, as in the following:
Label empNameLbl = new Label("My First Label");
The value of the label can be changed. However, changing the text of a label is not as easy as changing the text of a
string. For instance, the following will generate an error:
empNameLbl = "New Text";
 
Search WWH ::




Custom Search