KEMBAR78
JAVA GUI PART III | PDF
» GUI
˃ Events
˃ Event Handling

https://www.facebook.com/Oxus20

oxus20@gmail.com

JAVA
GUI
PART III
Milad Kawesh
Agenda
» Events
» Java Delegation Model
» Event Handling
» Practical Examples
2

https://www.facebook.com/Oxus20
Windows based Java Programs
» Console-Based Programming
˃ Every thing is predetermined
˃ The program code determines the sequence of events

» Window-Based Programming
˃ The operation is driven by what you do with the GUI
˃ Selecting menu items, buttons, or keyboard causes particular
actions within a program
˃ The specific program that is executed next is not known
https://www.facebook.com/Oxus20

3
Event driven Programming
» The signals that a program receives from the OS as a
result of your actions are called events
» A window based program is called event driven
program
» Unlike the old rigid old sequential programs,
» it puts user in charge, user control the sequence of
program
» Application waits for the user action
» This approach is called event driven programming
4

https://www.facebook.com/Oxus20
The Event Handling Process
» Suppose a user clicks a button in the GUI
˃ Button is the source of the event

» When a button is clicked, it will create a new
object that have information about event and
its source (in this case ActionListener)
» This object is passed to a method that handles
the event in its listener
» A listener is called Target of an event
5

https://www.facebook.com/Oxus20
Delegation Event Model
» The way in which events are handled in Java, using listener
objects, is called delegation event model
» We can make objects of any class listener objects by making
the class implement a listener interface
» In case of the button, the ActionListener interface needs to

be implemented to receive events from button
» actionPerformed(ActionEvent e) is called when the event
occurs and the event object is passed as an argument
https://www.facebook.com/Oxus20

6
Java Events
» Events are objects
˃ Objects that represent user initiated actions
˃ Examples:
+ button clicked -> ActionEvent
+ mouse dragged -> MouseEvent
+ Enter key pressed -> KeyEvent
˃ EventObject; root event class for all event objects
˃ AWTEvent; root event class for all AWT events
˃ Package java.awt.event
+ Provides interfaces and classes for dealing with different
types of events fired by AWT components.
7

https://www.facebook.com/Oxus20
8

https://www.facebook.com/Oxus20
Event Handling
» Programmer choice to decide how to handle the
generated event
˃ Ignore the Event
˃ Have the Event handled by the component where the
event was generated (Self Contained Event handling)
˃ Delegate event handling to some other object called
Listeners (Event Delegation)
9

https://www.facebook.com/Oxus20
Event Delegation
» Some time component on which event was
generated is not best suited to handle its own
event
» The process of assigning an object to handle a

component’s events is called delegation.
» The event handling objects are called Listeners
10

https://www.facebook.com/Oxus20
Key Methods of Event
» Object getSource()
In ObjectEvent, return the component in
which event took place.
» int getID()

In AwtEvent, return int that describes the
nature of the event e.g. on MouseEvent it will
give MOUSE_PRESSED, MOUSE_DRAGGED.
11

https://www.facebook.com/Oxus20
Event Listeners
» Interfaces to support dispatching of events
» Each Event class has a corresponding Listener interface
» Multiple listeners for the same event type
» Each interface will have one or more method

corresponding to types of events
» ActionEvent -> ActionListener
» MouseEvent -> MouseListener and MouseMotionListener
https://www.facebook.com/Oxus20

12
Registering Listeners
» Listeners register themselves with component
˃ public void addXXXListener(XXXListener)
˃ addActionListener, addItemListener, etc.

» Multiple listeners can be registered for the same

event on a component
˃ One event can trigger numerous responses
˃ Events are broadcast to all listeners
13

https://www.facebook.com/Oxus20
Wiring a Listener
» Define a class to implement the Listener Interface
Public class Applet extends Applet implements ActionListener
public class MyClass implements ActionListener {

» Add the implementation of the Interface
…
public void actionPerformed(ActionEvent e) {
// here’s where I do stuff when the action happens
…

» Add class as a Listener to Component
…
Button ok = new Button(“OK”)
ok.addActionListener(this);
14

https://www.facebook.com/Oxus20
ActionListener Example
import
import
import
import
import

javax.swing.*;
java.awt.FlowLayout;
java.awt.Dimension;
java.awt.event.ActionListener;
java.awt.event.ActionEvent;

public class ActionListenerTest extends JFrame implements
ActionListener {

JTextArea topTextArea;
JTextArea bottomTextArea;
JButton button1, button2;
final static String newline = "n";
15

https://www.facebook.com/Oxus20
ActionListener Example (cont.)
public ActionListenerTest() {
setLayout(new FlowLayout());
topTextArea = new JTextArea();
topTextArea.setEditable(false);
JScrollPane topScrollPane = new JScrollPane(topTextArea);
topScrollPane.setPreferredSize(new Dimension(200, 75));
add(topScrollPane);
bottomTextArea = new JTextArea();
bottomTextArea.setEditable(false);
JScrollPane bottomScrollPane = new JScrollPane(bottomTextArea);

bottomScrollPane.setPreferredSize(new Dimension(200, 75));
16

https://www.facebook.com/Oxus20
ActionListener Example (cont.)
add(bottomScrollPane);
button1 = new JButton("top Text area");
add(button1);
button2 = new JButton("down text area");
add(button2);
button1.addActionListener(this);
button2.addActionListener(this);
setSize(300, 222);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
17

https://www.facebook.com/Oxus20
ActionListener Example(cont.)
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
topTextArea.append(e.getActionCommand() + newline);
}
if (e.getSource() == button2) {
bottomTextArea.append(e.getActionCommand() + newline);
}
}
public static void main(String[] args) {
new ActionListenerTest();
}
}
18

https://www.facebook.com/Oxus20
OUTPUT

19

https://www.facebook.com/Oxus20
Lets practice

20

https://www.facebook.com/Oxus20
END

21

https://www.facebook.com/Oxus20

JAVA GUI PART III

  • 1.
    » GUI ˃ Events ˃Event Handling https://www.facebook.com/Oxus20 oxus20@gmail.com JAVA GUI PART III Milad Kawesh
  • 2.
    Agenda » Events » JavaDelegation Model » Event Handling » Practical Examples 2 https://www.facebook.com/Oxus20
  • 3.
    Windows based JavaPrograms » Console-Based Programming ˃ Every thing is predetermined ˃ The program code determines the sequence of events » Window-Based Programming ˃ The operation is driven by what you do with the GUI ˃ Selecting menu items, buttons, or keyboard causes particular actions within a program ˃ The specific program that is executed next is not known https://www.facebook.com/Oxus20 3
  • 4.
    Event driven Programming »The signals that a program receives from the OS as a result of your actions are called events » A window based program is called event driven program » Unlike the old rigid old sequential programs, » it puts user in charge, user control the sequence of program » Application waits for the user action » This approach is called event driven programming 4 https://www.facebook.com/Oxus20
  • 5.
    The Event HandlingProcess » Suppose a user clicks a button in the GUI ˃ Button is the source of the event » When a button is clicked, it will create a new object that have information about event and its source (in this case ActionListener) » This object is passed to a method that handles the event in its listener » A listener is called Target of an event 5 https://www.facebook.com/Oxus20
  • 6.
    Delegation Event Model »The way in which events are handled in Java, using listener objects, is called delegation event model » We can make objects of any class listener objects by making the class implement a listener interface » In case of the button, the ActionListener interface needs to be implemented to receive events from button » actionPerformed(ActionEvent e) is called when the event occurs and the event object is passed as an argument https://www.facebook.com/Oxus20 6
  • 7.
    Java Events » Eventsare objects ˃ Objects that represent user initiated actions ˃ Examples: + button clicked -> ActionEvent + mouse dragged -> MouseEvent + Enter key pressed -> KeyEvent ˃ EventObject; root event class for all event objects ˃ AWTEvent; root event class for all AWT events ˃ Package java.awt.event + Provides interfaces and classes for dealing with different types of events fired by AWT components. 7 https://www.facebook.com/Oxus20
  • 8.
  • 9.
    Event Handling » Programmerchoice to decide how to handle the generated event ˃ Ignore the Event ˃ Have the Event handled by the component where the event was generated (Self Contained Event handling) ˃ Delegate event handling to some other object called Listeners (Event Delegation) 9 https://www.facebook.com/Oxus20
  • 10.
    Event Delegation » Sometime component on which event was generated is not best suited to handle its own event » The process of assigning an object to handle a component’s events is called delegation. » The event handling objects are called Listeners 10 https://www.facebook.com/Oxus20
  • 11.
    Key Methods ofEvent » Object getSource() In ObjectEvent, return the component in which event took place. » int getID() In AwtEvent, return int that describes the nature of the event e.g. on MouseEvent it will give MOUSE_PRESSED, MOUSE_DRAGGED. 11 https://www.facebook.com/Oxus20
  • 12.
    Event Listeners » Interfacesto support dispatching of events » Each Event class has a corresponding Listener interface » Multiple listeners for the same event type » Each interface will have one or more method corresponding to types of events » ActionEvent -> ActionListener » MouseEvent -> MouseListener and MouseMotionListener https://www.facebook.com/Oxus20 12
  • 13.
    Registering Listeners » Listenersregister themselves with component ˃ public void addXXXListener(XXXListener) ˃ addActionListener, addItemListener, etc. » Multiple listeners can be registered for the same event on a component ˃ One event can trigger numerous responses ˃ Events are broadcast to all listeners 13 https://www.facebook.com/Oxus20
  • 14.
    Wiring a Listener »Define a class to implement the Listener Interface Public class Applet extends Applet implements ActionListener public class MyClass implements ActionListener { » Add the implementation of the Interface … public void actionPerformed(ActionEvent e) { // here’s where I do stuff when the action happens … » Add class as a Listener to Component … Button ok = new Button(“OK”) ok.addActionListener(this); 14 https://www.facebook.com/Oxus20
  • 15.
    ActionListener Example import import import import import javax.swing.*; java.awt.FlowLayout; java.awt.Dimension; java.awt.event.ActionListener; java.awt.event.ActionEvent; public classActionListenerTest extends JFrame implements ActionListener { JTextArea topTextArea; JTextArea bottomTextArea; JButton button1, button2; final static String newline = "n"; 15 https://www.facebook.com/Oxus20
  • 16.
    ActionListener Example (cont.) publicActionListenerTest() { setLayout(new FlowLayout()); topTextArea = new JTextArea(); topTextArea.setEditable(false); JScrollPane topScrollPane = new JScrollPane(topTextArea); topScrollPane.setPreferredSize(new Dimension(200, 75)); add(topScrollPane); bottomTextArea = new JTextArea(); bottomTextArea.setEditable(false); JScrollPane bottomScrollPane = new JScrollPane(bottomTextArea); bottomScrollPane.setPreferredSize(new Dimension(200, 75)); 16 https://www.facebook.com/Oxus20
  • 17.
    ActionListener Example (cont.) add(bottomScrollPane); button1= new JButton("top Text area"); add(button1); button2 = new JButton("down text area"); add(button2); button1.addActionListener(this); button2.addActionListener(this); setSize(300, 222); setResizable(false); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } 17 https://www.facebook.com/Oxus20
  • 18.
    ActionListener Example(cont.) public voidactionPerformed(ActionEvent e) { if (e.getSource() == button1) { topTextArea.append(e.getActionCommand() + newline); } if (e.getSource() == button2) { bottomTextArea.append(e.getActionCommand() + newline); } } public static void main(String[] args) { new ActionListenerTest(); } } 18 https://www.facebook.com/Oxus20
  • 19.
  • 20.
  • 21.