Module-5
Module-5
Event Model
• To comprehend the role of adapter classes, one must first understand the
concept of event listeners in Java. An event listener is an interface that contains
methods invoked when certain events occur.
• For instance, the WindowListener interface has seven different methods
corresponding to various window events, like window opening, closing,
deiconifying, etc. If a class implements this interface, it's required to provide
implementations for all seven methods, even if it's only interested in one event.
• This is where adapter classes come in handy. Since they provide default (empty)
implementations for all event handling methods, you can create a subclass from
an adapter class, and override only those methods you're interested in.
Java WindowAdapter
Java MouseAdapter
Java MouseMotionAdapter
Benefits of Java Adapter Class
Inner Classes
• Recall that an inner class is a class defined within another class, or even within an
expression. This section illustrates how inner classes can be used to simplify the code
when using event adapter classes.
• To understand the benefit provided by inner classes, consider the applet shown in the
following listing. It does not use an inner class. Its goal is to display the string "Mouse
Pressed" in the status bar of the applet viewer or browser when the mouse is pressed.
• There are two top-level classes in this program. MousePressedDemo extends Applet,
and MyMouseAdapter extends MouseAdapter.
• The init( ) method of MousePressedDemo instantiates MyMouseAdapter and provides
this object as an argument to the addMouseListener( ) method. Notice that a reference to
the applet is supplied as an argument to the MyMouseAdapter constructor. This
reference is stored in an instance variable for later use by the mousePressed( ) method.
When the mouse is pressed, it invokes the showStatus( ) method of the applet
Event-driven Programming and
Graphical User Interfaces
(GUIs) with Swing/AWT
Why learn GUIs?
• Learn about event-driven programming techniques
• Practice learning and using a large, complex API
• A chance to see how it is designed and learn from it:
• model-view separation
• design patterns
• refactoring vs. reimplementing an ailing API
• Because GUIs are neat!
• Container
• JComponent (Swing)
• JButton JColorChooser JFileChooser
• JComboBox JLabel JList
• JMenuBar JOptionPane JPanel
• JPopupMenu JProgressBar JScrollbar
• JScrollPane JSlider JSpinner
• JSplitPane JTabbedPane JTable
• JToolbar JTree JTextArea
• JTextField ...
Component properties
• Each has a get (or is) accessor and a set modifier
method.
• examples:
name getColor,
type setFont, setEnabled,
description
isVisible
background Color background color behind component
border Border border line around component
enabled boolean whether it can be interacted with
focusable boolean whether key text can be typed on it
font Font font used for text in component
foreground Color foreground color of component
height, width int component's current size in pixels
visible boolean whether component can be seen
tooltip text String text shown when hovering mouse
size, minimum / Dimension various sizes, size limits, or desired
maximum / preferred size sizes that the component may take
JFrame
a graphical window to hold other components
• public JFrame()
public JFrame(String title)
Creates a frame with an optional title.
frame.setVisible(true);
}
Sizing and positioning
How does the programmer specify where each
component appears, how big each component should
be, and what the component should do if the window is
resized / moved / maximized / etc.?
myFrame.setLayout(new FlowLayout());
myFrame.add(new JButton("Button 1"));
• EventListener
EventObject
• (AWT)
AWTEventListener
AWTEvent
• • ActionEvent
ActionListener
• • TextEvent
TextListener
• • ComponentEvent
ComponentListener
• • FocusEvent
FocusListener
• • WindowEvent
WindowListener
• InputEvent
• KeyEvent
• KeyListener
• MouseEvent
• MouseListener
Action events
• action event: An action that has occurred on a
GUI component.
• The most common, general event type in Swing.
Caused by:
• button or menu clicks,
• check box checking / unchecking,
• pressing Enter in a text field, ...
• Usefulness:
• Nested classes are hidden from other classes
(encapsulated).
• Nested objects can access/modify the fields of their
outer object.
• Only the outer class can see the nested class or make
objects of it.
• Each nested object is associated with the outer object that
created it, so it can access/modify that outer object's
methods/fields.
Static inner classes
// enclosing outer class
public class name {
...
public MyGUI() {
...
stutter.addActionListener(new StutterListener());
}
...