Java Reference
In-Depth Information
Components, layout, and event handling
The details involved in creating GUIs are extensive. In this topic, we shall not be able to cover
all details of all possible things you can do with them, but we shall discuss the general princi-
ples and a good number of examples.
All GUI programming in Java is done through the use of dedicated standard class libraries.
Once we understand the principles, we can find out all the necessary details by working with
the standard library documentation.
The principles we need to understand can be divided into three topic areas:
What kinds of elements can we show on screen?
How do we arrange those elements?
How do we react to user input?
We shall discuss these questions under the keywords components, layout, and event handling.
Components are the individual parts that a GUI is built from. They are things such as buttons, menus,
menu items, checkboxes, sliders, text fields, and so on. The Java library contains a good number of
ready-made components, and we can also write our own. We shall have to learn what the important
components are, how to create them, and how to make them look the way we want them to look.
A GUI is built by
arranging compo-
nents on screen.
Components are
represented by
Layout deals with the issue of how to arrange the components on screen. Older, more primitive
GUI systems handled this with two-dimensional coordinates: the programmer specified x - and
y -coordinates (in pixels) for the position and the size of each component. In more modern GUI
systems, this is too simplistic. We have to take into account different screen resolutions, differ-
ent fonts, users resizing windows, and many other aspects that make layout more difficult. The
solution will be a scheme where we can specify the layout in more general terms. We can, for
example, specify that a particular component should be below this other one or that this compo-
nent should be stretched if the window gets resized but this other should always have a constant
size. We shall see that this is done using layout managers .
Arranging the
layout of compo-
nents is achieved
by using layout
Event handling refers to the technique we shall use to deal with user input. Once we have cre-
ated our components and positioned them on screen, we also have to make sure that something
happens when a user clicks a button. The model used by the Java library for this is event-based:
if a user activates a component (e.g., clicks a button or selects a menu item) the system will
generate an event. Our application can then receive a notification of the event (by having one of
its methods invoked), and we can take appropriate action.
The term event
handling refers to
the task of react-
ing to user events,
such as mouse-
button clicks or
keyboard input.
We shall discuss each of these areas in much more detail later in this chapter. First, however, we
shall briefly introduce a bit more background and terminology.
AWT and Swing
Java has two GUI libraries. The older one is called AWT (Abstract Window Toolkit) and was
introduced as part of the original Java API. Later, a much-improved GUI library, called Swing,
was added to Java.
Search WWH ::

Custom Search