



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of design patterns, focusing on creational, structural, and behavioral patterns. It includes an example java program that demonstrates the use of these patterns through the creation of a simple gui application. The document also discusses the use of actionlistener and actionevent classes, as well as the differences between awt list class and jfc jlist class.
Typology: Study notes
1 / 7
This page cannot be seen from the preview
Don't miss anything!




1
Creational patterns
Structural patterns
Behavioral patterns
interact and distribute responsibility
3
Lets create a simple Java program that
allows you to enter names into a list,
be transferred to another list.and then select some of those names to
doing advanced work.and the second list, those who will beOur initial list consists of a class roster
In the example UI,
field and click onYou enter names into the top entry
Insert
(^) to move the
names into the left-hand list box.
onbox, you click on them, and then clickTo move names to the right-hand list
Add
.
Remove list box, click on it and then onTo remove a name from the right hand
. This moves the name back to
the left-hand list.
constructor and anThe code will consist of a GUI creation
actionListener
routine for the
three buttons:
5
The listener interface for receiving action events.
component'sthat class is registered with a component, using theimplements this interface, and the object created with
addActionListener
method. When the
action event occurs, that object's
actionPerformed
method is invoked.
public class ActionEvent extends AWTEvent
A semantic event which indicates that a component-defined action occurred.
component-specific action occurs (such as being pressed).This high-level event is generated by a component (such as a Button) when the
such events using the component's addActionListener method.The event is passed to every ActionListener object that registered to receive
when the event occurs.The object that implements the ActionListener interface gets this ActionEvent
(semantic) event like "button pressed".movements and mouse clicks, and can instead process a "meaningful"The listener is therefore spared the details of processing individual mouse
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ActionEvent
7
Method inherited from class java.util.EventObject
public Object getSource()
occurred.
add() is a method of AWT
List
class.
13
vector or other ListModel class associated with it.The JList class is a window container which has an array,
the data.It is this ListModel that actually contains and manipulates
class.relies on being inserted in the viewport of the JScrollPaneThe JList class does not contain a scroll bar, but instead
as long as you provide the cell drawing routine for them.limited to strings, but may be almost any kind of objects,Data in the JList class and its associated ListModel are not
illustrating each choice in the list.This makes it possible to have list boxes with pictures
that in this simple case, needs only the three methods:We are only going to create a class that emulates the List class, and
add(String);
remove(String);
String[] getSelectedItems()
the class we create implements those methods:We define the needed methods as an interface and then make sure that
15
interface.which implements the methods of the awtListWe create a class that contains a JList class but
all, but the JScrollPane that encloses it.outer container for a JList is not the list element atThis is a pretty good choice here, because the
17
methods:AbstractListModel, which defines the followingthe JListData class. This class is derived from theNote that the actual data handling takes place in
19
the actual displayed list data.path between the data stored in the ListModel andThe three fire methods are the communication
updated.Firing them causes the displayed list to be
Hence, Each time we add data to the
data
vector,
we call the
fireIntervalAdded
method to tell the
list.list display to refresh that area of the displayed
25
must adapt to: reflection.Java has yet another way for adapters to recognize which of several classes it
parameters for any class.You can use reflection to discover the names of public methods and their
For example, for any arbitrary object you can use the
getClass()
(^) method to
obtain its class and the
getMethods()
method to obtain an array of the method
names.
very large list of methods.code shown in the previous slide can generate aA “method dump” like the one produced by the
which arguments that method requires.you are looking for and simply want to find outIt is easier if you know the name of the method
the adapting you need to carry out.From that method signature, you can then deduce