Graphical User Interfaces and Event Handling
Abstract Window Toolkit, the java.awt and java.awt.event packages
Simon Kroly
simon.karoly@codespring.ro
Abstract Window Toolkit
Abstraction: the same classes can be used, and the same
functionalities can be provided for all (supported) platforms. From
the programmer's viewpoint abstract Java components are used
(different platforms, the same code), but native components are
displayed for the user. This cross-platform behavior is realized by
platform-specific toolkits.
Toolkit: component factory
java.awt.peer interfaces for different component types
2016. 07. 11. 15:35 AWT GUI and event handling 2
AWT class hierarchy
Component: abstract methods (imple-
mented by subclasses corresponding to
concrete component types), common
properties (e.g. color, size etc.) and
methods
Container classes: containing other
components (e.g. panel, window etc.)
Functioning: the communication and
functioning is based on events. The
registered recipients (listener objects) are
notified by an AWT thread about user
interactions (e.g. mouse click).
2016. 07. 11. 15:35 AWT GUI and event handling 3
Model View - Controller
MVC design pattern: for separating the model (data managed by the
application), the view (visualizing data on a GUI) and the controller
(handling events, responding to user interactions) increased flexibility,
maintainability.
A simple example: a button
Model: a logical value (indicating the current state)
View: the visual representation (position, size, color etc.)
Controller: handling the events (button clicks), responding to user requests
Functioning (general):
A user interaction is detected (e.g. a button is pressed)
A corresponding event is received by the registered listeners
The proper controller is called by the listener
If it is required, the model is updated and the view is refreshed by the
controller (eventually, the controller communicates with BL components)
The refreshed view waits for further interactions
2016. 07. 11. 15:35 AWT GUI and event handling 4
A simple example
A frame with two buttons and a label:
import java.awt.Frame;
import java.awt.Button;
import java.awt.Label;
import java.awt.BorderLayout;
public class MyFrame extends Frame {
private Button buttons[];
private Label label;
public MyFrame(){
// creating buttons
buttons = new Button[2];
buttons[0] = new Button("Button 1");
add(buttons[0], BorderLayout.NORTH);
buttons[1] = new Button("Button 2");
add(buttons[1], BorderLayout.SOUTH);
// creating the label
label = new Label("Label");
add(label, BorderLayout.CENTER);
}
public static void main(String[] args) {
MyFrame f = new MyFrame();
f.setBounds(10,10, 300, 300);
f.setVisible(true);
}
2016. 07.
} 11. 15:35 AWT GUI and event handling 5
Layout managers
The arrangement of the components within the containers is realized by
layout manager objects. There are several layout manager classes
provided by the AWT framework: FlowLayout, GridLayout, CardLayout,
BorderLayout, GridBagLayout. Third-party classes or own
implementations can be also used.
FlowLayout fLayout = new FlowLayout();
setLayout(fLayout);
Button okButton = new Button("Ok");
add(okButton);
Button cancelButton = new Button("Cancel");
add(cancelButton);
FlowLayout fLayout = new FlowLayout();
fLayout.setAlignment(FlowLayout.LEFT)
setLayout(fLayout);
setLayout(new BorderLayout(10, 6));
add(new Button("Egy"), BorderLayout.NORTH);
add(new Button("Ketto"), BorderLayout.EAST);
add(new Button("Harom"), BorderLayout.SOUTH);
add(new Button("Negy"), BorderLayout.CENTER);
2016. 07. 11. 15:35 AWT GUI and event handling 6
Event handling
The communication between the UI components is based on
events. In fact, these events can be considered as messages/
notifications (e.g. the application is notified by a button if it is
clicked by the user).
The communication is based on the observable observer model.
An event has a source, and one or more receptors.
All event handling methods are implemented by the receptors.
Events: ActionEvent, MouseEvent etc.
Event object
Source Listener
registration
2016. 07. 11. 15:35 AWT GUI and event handling 7
Event handling
Event object
Source Listener
registration
At registration the listener object is added to a list. The list contains
all the receptors "interested" in receiving events from that source.
The event is propagated to the recipient (listener object) by calling
a proper handler method. These methods are declared in listener
interfaces (implemented by the classes of the listener objects).
E.g. button click: ActionEvent ActionListener public void
actionPerformed(ActionEvent))
2016. 07. 11. 15:35 AWT GUI and event handling 8
Event handling - example
Button can be a source for action events:
Button b = new Button(OK);
Recipient:
class Receiver implements ActionListener{
private void setupReceiver(){
...
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
// operations performed after a button click
}
}
The source provides possibility for registering listeners. In this case
the following two methods are provided by the Button class:
public void addActionListener(ActionListener listener){...}
public void removeActionListener(ActionListener listener){...}
2016. 07. 11. 15:35 AWT GUI and event handling 9
AWT events - hierarchy
java.util java.awt
EventObject AWTEvent
EventListener
java.awt.event
ActionEvent ItemEvent
ComponentListener ActionListener
AdjustmentEvent TextEvent
FocusListener ItemListener
ComponentEvent
KeyListener AdjustmentListener
ContainerEvent WindowEvent
MouseListener TextListener
FocusEvent InputEvent
ContainerListener WindowListener
MouseEvent
KeyEvent
MouseMotionListener
2016. 07. 11. 15:35 AWT GUI and event handling 10
AWT events
Event Possible sources Listener interface Handler methods
componentResized()
componentMoved()
ComponentEvent All components ComponentListener
componentShown()
componentHidden()
focusGained()
FocusEvent All components FocusListener
focusLost()
keyTyped()
KeyEvent All components KeyListener keyPressed()
keyReleased()
mouseClicked()
MouseListener
mousePressed()
mouseReleased()
MouseEvent All components mouseEntered()
mouseExited()
MouseMotionListener
mouseDragged()
mouseMoved()
componentAdded()
ContainerEvent All containers ContainerListener
componentRemoved()
2016. 07. 11. 15:35 AWT GUI and event handling 11
AWT events
Event Possible sources Listener interface Handler methods
TextField
MenuItem
ActionEvent ActionListener actionPerformed()
List
Button
List
CheckBox
ItemEvent ItemListener itemStateChanged()
Choice
CheckboxMenuItem
ScrollPane
AdjustmentEvent AdjustmentListener adjustmentValueChanged()
Scrollbar
TextArea
TextEvent TextListener textValueChanged()
TextField
windowOpened()
windowClosing()
windowClosed()
Frame
WindowEvent WindowListener windowIconified()
Dialog
windowDeiconified()
windowActivated()
2016. 07. 11. 15:35 AWT GUI and event handling windowDeactivated() 12
InputEvent
java.awt.event.InputEvent
Constants: SHIFT_MASK, CTRL_MASK, META_MASK, ALT_MASK,
BUTTON1_MASK, BUTTON2_MASK, BUTTON#_MASK stb. +
methods
public void mousePressed( MouseEvent e){
int mods = e.getModifiers();
if( ( mods & InputEvent.SHIFT_MASK) != 0 ){
// SHIFT is pressed
...
}
}
2016. 07. 11. 15:35 AWT GUI and event handling 13
Listener classes
Example: vending machine with
3 buttons: coffee, tea, refresher drink
Without separate listener classes:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == teaButton)
else if
}
Could be disadvantageous when creating complex UIs + the application
logic is mixed with the visualization (it is against the MVC pattern).
Could be a better solution:
class CoffeeAdapter implements ActionListener {
MachineController c;
CoffeeAdapter(MachineController c) {
this.c = c;
}
public void actionPerformed(ActionEvent e) {
c.coffee();
}
}
MachineController c;
coffeeButton.addActionListener(new CoffeeAdapter(c));
2016. 07. 11. 15:35 AWT GUI and event handling 14
Anonymous listener classes
myComponent.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e ) {
...
}
});
2016. 07. 11. 15:35 AWT GUI and event handling 15