SlideShare una empresa de Scribd logo
1 de 40
EVENT HANDLER
Chapter 11.5:
Event-Driven Programming
 In event-driven programming, code is executed
upon activation of events.
 Ex: a button click, or mouse movement
Events and Event Source
 An event can be defined as a type of signal to the
program that something has happened.
 The event is generated by external user actions
such as mouse movements, mouse button clicks,
and keystrokes, or by the operating system, such
as a timer.
 The component on which an event is generated is
called the source object.
 Ex: source object: Button for a button-clicking action
event.
Event Classes
AWTEventEventObject
AdjustmentEvent
ComponentEvent
TextEvent
ItemEvent
ActionEvent
InputEvent
WindowEvent
MouseEvent
KeyEvent
ContainerEvent
FocusEvent
PaintEvent
ListSelectionEvent
Subclasses an event is an object of the EventObject class
Event Information
 An event object contains whatever
properties are pertinent to the event.
 You can identify the source object of the
event using the getSource() instance
method in the EventObject class.
 The subclasses of EventObject deal with
special types of events, such as button
actions, window events, component
events, mouse movements, and
keystrokes.
Selected User Actions
Source Event Type
User Action Object Generated
Click a button JButton ActionEvent
Click a check box JCheckBox ItemEvent, ActionEvent
Click a radio button JRadioButton ItemEvent, ActionEvent
Press Enter on a text field JTextField ActionEvent
Select a new item JComboBox ItemEvent, ActionEvent
Select item(s) JList ListSelectionEvent
Select a menu item JMenuItem ActionEvent
Window opened, closed, etc. Window WindowEvent
Mouse pressed, released, etc. Component MouseEvent
Key released, pressed, etc. Component KeyEvent
In detail, refer to Table 16.1 pg 559
Listeners, Registrations, and
Handling Events
 Java uses a delegation-based model for event
handling
 An external user action on a source object triggers an
event
 An object interested in the event receives the event
 The latter object is called a listener
Listeners, Registrations, and
Handling Events
 Two things are needed for an object to be a listener for an
event on a source object:
1. The listener object’s class must implement the corresponding
event-listener interface. Java provides a listener interface for
every type of GUI event.
The listener interface is usually named XListener for XEvent ,
Ex: Listener interface for ActionEvent is ActionListener
each listener for ActionEvent should implement the
ActionListener interface
Selected Event Handlers
Event Class Listener Interface Listener Methods (Handlers)
ActionEvent ActionListener actionPerformed(ActionEvent)
ItemEvent ItemListener itemStateChanged(ItemEvent)
WindowEvent WindowListener windowClosing(WindowEvent)
windowOpened(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
ContainerEvent ContainerListener componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
MouseEvent MouseListener mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseClicked(MouseEvent)
mouseExited(MouseEvent)
mouseEntered(MouseEvent)
KeyEvent KeyListener keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTypeed(KeyEvent)
In detail, refer to Table 16.2 pg 561
Listeners, Registrations, and
Handling Events
 Two things are needed for an object to be a listener for an
event on a source object:
2. The listener object must be registered by the source object.
Registration methods are dependent on the event type
Ex: For ActionEvent, the method is addActionListener
In general: the method is named addXListener for Xevent
Listeners, Registrations, and
Handling Events
 For each event, the source object maintains a list
of listeners and notifies all the registered listeners
by invoking the handler on the listener object to
respond to the event.
The Delegation Model: Example
source: JButton
+addActionListener(ActionListener listener)
listener: ListenerClass
ActionListener
+actionPerformed(ActionEvent event)
Register by invoking
source.addActionListener(listener);
ListenerClass listener = new ListenerClass();
JButton jbt = new JButton("OK");
jbt.addActionListener(listener);
class ListenerClass implements ActionListener {
public void actionPerformed(ActionEvent e) {
S.o.p (“Button “+ e.getActionCommand()+ “ is
clicked.”);}
Example:
 Class must implement the listener interface (ex: ActionListener).
and method to handle the event (ex: actionPerformed())
 The listener object must register with source object (ex: button)
 Done by invoking the addActionListener in the button object
 Ex::
public class TestActionButton extends JFrame implements ActionListener
{
JButton btn = new JButton(“Click Here");
btn.addActionListener(this);
public void actionPerformed(ActionEvent e)
{
}
}
Registration method (XEvent  addXListener)
ActionEvent
 The components that involved with this event
are:
 JButton – clicka a button
 JMenuItem – select a menu item
 JTextField – press return on a text field
 Event listener interface – ActionListener
 Event handler method
public void actionPerformed(ActionEvent e)
java.awt.event.ActionEvent
java.awt.event.ActionEvent
+getActionCommand(): String
+getModifier(): int
+getWhen(): long
Returns the command string associated with this action. For a
button, its text is the command string.
Returns the modifier keys held down during this action event.
Returns the timestamp when this event occurred. The time is
the number of milliseconds since January 1, 1970, 00:00:00
GMT.
java.util.EventObject
+getSource(): Object Returns the object on which the event initially occurred.
java.awt.event.AWTEvent
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiActionButang extends JFrame implements ActionListener
{
JButton btg = new JButton("Sila Klik");
JTextField txtField = new JTextField(10);
JLabel lbl = new JLabel(" ");
public UjiActionButang()
{
super("Melaksanakan ActionEvent");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
bekas.add(btg);
btg.addActionListener(this);
bekas.add(txtField);
bekas.add(lbl);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
lbl.setText("Terima Kasih");
txtField.setText("Sama-sama");
}
}
public static void main(String[] arg)
{
UjiActionButang teks = new UjiActionButang();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
To display label, and text on text field
when button is clicked
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ActionTextField extends JFrame implements ActionListener
{
JLabel lbl = new JLabel("Nama ");
JTextField txtField = new JTextField(10);
public ActionTextField()
{
super("Melaksanakan ActionEvent- TextField");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
bekas.add(lbl);
bekas.add(txtField);
txtField.addActionListener(this);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== txtField)
{
JOptionPane.showMessageDialog(this,"Nama anda = "+
txtField.getText(),"Pemberitahuan",
JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
ActionTextField teks = new ActionTextField();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
To display name from text field on dialog box
when pressing <enter> on text field
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class ActionButton extends JFrame implements ActionListener
{
JLabel lbl = new JLabel("Nama ");
JTextField txtField = new JTextField(10);
JButton btg = new JButton("Klik");
JTextArea txtArea = new JTextArea("Ali",10,6);
public ActionButton()
{
super("Melaksanakan ActionEvent- Butang");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
bekas.add(lbl);
bekas.add(txtField);
bekas.add(btg);
bekas.add(txtArea);
btg.addActionListener(this);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
txtArea.append("n"+txtField.getText());
}
}
public static void main(String[] arg)
{
ActionButton teks = new ActionButton();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
To append text on text area when button is
clicked
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiActionMenu extends JFrame implements ActionListener
{
JFrame frame = new JFrame();
JMenuBar jmb = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem baru= new JMenuItem("New");
JMenuItem buka= new JMenuItem("Open");
public UjiActionMenu()
{
frame.setTitle("membuat menu");
baru.addActionListener(this);
fileMenu.add(baru);
fileMenu.add(buka);
jmb.add(fileMenu);
frame.setJMenuBar(jmb);
frame.setSize(400,200);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == baru)
JOptionPane.showMessageDialog(this,"Anda telah memilih menu New",
"Pemberitahuan",JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] arg)
{
UjiActionMenu teks = new UjiActionMenu();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Display dialog box when menu item is clicked
comboBox
 Generate two types of event
 ActionEvent
 ItemEvent
 For the case of ItemEvent:
 Event listener Interface : ItemListener
 Event handler method
 public void ItemStateChanged(ItemEvent e)
○ When select an item in the combo box
 Index for the first item in the combo box will be
started by 0..n-1.
comboBox
javax.swing.JComboBox
+JComboBox()
+JComboBox(items: Object[])
+addItem(item: Object): void
+getItemAt(index: int): Object
+getItemCount(): int
+getSelectedIndex(): int
+setSelectedIndex(index: int): void
+getSelectedItem(): Object
+setSelectedItem(item: Object): void
+removeItem(anObject: Object): void
+removeItemAt(anIndex: int): void
+removeAllItems(): void
Creates a default empty combo box.
Creates a combo box that contains the elements in the specified array.
Adds an item to the combo box.
Returns the item at the specified index.
Returns the number of items in the combo box.
Returns the index of the selected item.
Sets the selected index in the combo box.
Returns the selected item.
Sets the selected item in the combo box.
Removes an item from the item list.
Removes the item at the specified index in the combo box.
Removes all items in the combo box.
javax.swing.JComponent
Using the
itemStateChanged Handler
public void itemStateChanged(ItemEvent e)
{
// Make sure the source is a combo box
if (e.getSource() instanceof JComboBox)
String s = (String)e.getItem();
}
When a new item is selected, itemStateChanged() for
ItemEvent is invoked .
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiItemEventComboBox extends JFrame
implements ItemListener
{
JComboBox jcb = new JComboBox();
public UjiItemEventComboBox()
{
super("Membuat combobox");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
jcb.addItem("wira");
jcb.addItem("waja");
jcb.addItemListener(this);
bekas.add(jcb);
setSize(400,200);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()== jcb)
{
if (e.getStateChange() == ItemEvent.SELECTED)
{
String s1 = (String) jcb.getSelectedItem();
JOptionPane.showMessageDialog(this,"Anda telah memilih "+s1,"Maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
}
ItemEvent: Display item on dialog box when
item in Combo Box is selected
public static void main(String[] arg)
{
UjiItemEventComboBox teks = new UjiItemEventComboBox();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiActionEventComboBox extends JFrame implements ActionListener
{
JComboBox jcb = new JComboBox();
JButton btg = new JButton("Klik");
public UjiActionEventComboBox()
{
super("Membuat combobox");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
jcb.addItem("wira");
jcb.addItem("waja");
btg.addActionListener(this);
bekas.add(jcb);
bekas.add(btg);
setSize(400,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
String s1 = (String)jcb.getSelectedItem();
JOptionPane.showMessageDialog(this,"Anda telah memilih "+s1,“
Maklumat",JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
UjiActionEventComboBox teks = new UjiActionEventComboBox();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ActionEvent: Display selected item on dialog
box when a button is clicked
ListSelectionEvent - JList
 Generates
javax.swing.event.ListSelectionEvent to
notify the listeners of the slections
 Event listener interface :
ListSelectionListener
 Event handler interface:
 public void
valueChanged(ListSelectionEvent e)
○ When select item(s) in JList
 Index for the first item in the JList will
be started by 0..n-1.
ListSelectionEvent - JList
javax.swing.JList
+JList()
+JList(items: Object[])
+getSelectedIndex(): int
+setSelectedIndex(index: int): void
+getSelectedIndices(): int[]
+setSelectedIndices(indices: int[]): void
+getSelectedValue(): Object
+getSelectedValues(): Object[]
+getVisibleRowCount(): int
+setVisibleRowCount(count: int): void
+getSelectionBackground(): Color
+setSelectionBackground(c: Color): void
+getSelectionForeground(): Color
+setSelectionForeground(c: Color): void
+getSelectionMode(): int
+setSelectionMode(selectionMode: int):
Creates a default empty list.
Creates a list that contains the elements in the specified array.
Returns the index of the first selected item.
Selects the cell at the specified index.
Returns an array of all of the selected indices in increasing order.
Selects the cells at the specified indices.
Returns the first selected item in the list.
Returns an array of the values for the selected cells in increasing index order.
Returns the number of visible rows displayed without a scrollbar. (default: 8)
Sets the preferred number of visible rows displayed without a scrollbar.
Returns the background color of the selected cells.
Sets the background color of the selected cells.
Returns the foreground color of the selected cells.
Sets the foreground color of the selected cells.
Returns the selection mode for the list.
Sets the selection mode for the list.
javax.swing.JComponent
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class UjiSelectionEventList extends JFrame implements ListSelectionListener
{
String[] jenisWarna= {"merah","hijau","kuning","jingga","biru"};
JList senaraiWarna;
public UjiSelectionEventList()
{
super("Membuat JList");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
senaraiWarna = new JList(jenisWarna);
senaraiWarna.addListSelectionListener(this);
senaraiWarna.setVisibleRowCount(3);
senaraiWarna.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
bekas.add(new JScrollPane(senaraiWarna));
setSize(300,200);
setVisible(true);
}
public void valueChanged(ListSelectionEvent e)
{
if (e.getSource()== senaraiWarna)
{
String s1 = (String)senaraiWarna.getSelectedValue();
JOptionPane.showMessageDialog(this,s1,"maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
SelectionEvent: Display selected item when item
in JList is clicked
public static void main(String[] arg)
{
UjiSelectionEventList list = new UjiSelectionEventList();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class DataListEventt extends JFrame implements ActionListener
{
String[] jenisWarna= {"merah","hijau","kuning","jingga","biru"};
JList senaraiWarna;
JButton btg = new JButton("Klik");
public DataListEventt()
{
super("Membuat JList");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
senaraiWarna = new JList(jenisWarna);
senaraiWarna.setVisibleRowCount(3);
senaraiWarna.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
bekas.add(new JScrollPane(senaraiWarna));
bekas.add(btg);
btg.addActionListener(this);
setSize(300,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
String s1 = (String)senaraiWarna.getSelectedValue();
JOptionPane.showMessageDialog(this,s1,"maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
ActionEvent: Display selected item in JList
when a button is clicked
public static void main(String[] arg)
{
DataListEventt list = new DataListEventt();
list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ItemEvent - RadioButton
 Event Listener Interface : ItemListener
 Event handler method :
 public void ItemStateChanged(ItemEvent e)
○ Click a radio button
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiItemListenerRadioButton extends JFrame implements ItemListener
{
JRadioButton rdButton1,rdButton2;
public UjiItemListenerRadioButton()
{
super("Membuat RadioButton");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
rdButton1 = new JRadioButton("wira");
rdButton2 = new JRadioButton("Waja",true);
rdButton1.addItemListener(this);
rdButton2.addItemListener(this);
bekas.add(rdButton1);
bekas.add(rdButton2);
ButtonGroup butang = new ButtonGroup();
butang.add(rdButton1);
butang.add(rdButton2);
setSize(400,200);
setVisible(true);
}
ItemEvent: Display item on dialog box when a
radio button is clicked
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()== rdButton1)
{
if (e.getStateChange() == ItemEvent.SELECTED)
JOptionPane.showMessageDialog(this,"kereta wira","Maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
if (e.getSource()== rdButton2)
{
if (e.getStateChange() == ItemEvent.SELECTED)
JOptionPane.showMessageDialog(this,"kereta waja","Maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
UjiItemListenerRadioButton teks = new UjiItemListenerRadioButton();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DataRadioButton extends JFrame implements ActionListener
{
JRadioButton rdButton1,rdButton2;
JButton btg = new JButton("Klik");
String kereta;
public DataRadioButton()
{
super("Membuat RadioButton");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
rdButton1 = new JRadioButton("wira");
rdButton2 = new JRadioButton("Waja",true);
bekas.add(rdButton1);
bekas.add(rdButton2);
bekas.add(btg);
btg.addActionListener(this);
ButtonGroup butang = new ButtonGroup();
butang.add(rdButton1);
butang.add(rdButton2);
setSize(400,200);
setVisible(true);
}
ActionEvent: Display selected item from radio
button when a button is clicked
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
if (rdButton1.isSelected())
kereta = "Wira";
if (rdButton2.isSelected())
kereta = "Waja";
JOptionPane.showMessageDialog(this,kereta,"maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
DataRadioButton teks = new DataRadioButton();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ItemEvent - CheckBox
 Event Listener Interface : ItemListener
 Event handler metod :
 public void ItemStateChanged(ItemEvent e)
○ Click a check box
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class UjiItemEventCheckBox extends JFrame implements ItemListener
{
JCheckBox chkBox1,chkBox2;
public UjiItemEventCheckBox()
{
super("Membuat CheckBox");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
chkBox1 = new JCheckBox("Wira");
chkBox1.addItemListener(this);
chkBox2 = new JCheckBox("Waja");
bekas.add(chkBox1);
bekas.add(chkBox2);
setSize(400,200);
setVisible(true);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()== chkBox1)
{
if (e.getStateChange() == ItemEvent.SELECTED)
JOptionPane.showMessageDialog(this,"Di tanda","Maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
UjiItemEventCheckBox teks = new UjiItemEventCheckBox();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
ItemEvent: Display a message when clicking a
check box
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DataCheckBox extends JFrame implements ActionListener
{
JCheckBox chkBox1,chkBox2;
JButton btg = new JButton("Klik");
String kereta;
public DataCheckBox()
{
super("Membuat CheckBox");
Container bekas = getContentPane();
bekas.setLayout(new FlowLayout());
chkBox1 = new JCheckBox("Wira");
chkBox2 = new JCheckBox("Waja");
bekas.add(chkBox1);
bekas.add(chkBox2);
bekas.add(btg);
btg.addActionListener(this);
setSize(400,200);
setVisible(true);
}
ActionEvent: Display selected item from check
box when button is clicked
public void actionPerformed(ActionEvent e)
{
if (e.getSource()== btg)
{
if (chkBox1.isSelected())
kereta = "Wira";
if (chkBox2.isSelected())
kereta = "Waja";
JOptionPane.showMessageDialog(this,kereta,"maklumat",
JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] arg)
{
DataCheckBox teks = new DataCheckBox();
teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Más contenido relacionado

La actualidad más candente

tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event HandlingShraddha
 
Event handling
Event handlingEvent handling
Event handlingswapnac12
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 
Event handling63
Event handling63Event handling63
Event handling63myrajendra
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART IIIOXUS 20
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to buttonyugandhar vadlamudi
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
 
Jquery Example PPT
Jquery Example PPTJquery Example PPT
Jquery Example PPTKaml Sah
 

La actualidad más candente (19)

tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Event handling in Java(part 2)
Event handling in Java(part 2)Event handling in Java(part 2)
Event handling in Java(part 2)
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Java Event Handling
Java Event HandlingJava Event Handling
Java Event Handling
 
Event handling
Event handlingEvent handling
Event handling
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Event handling63
Event handling63Event handling63
Event handling63
 
Jp notes
Jp notesJp notes
Jp notes
 
Swing
SwingSwing
Swing
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Java gui event
Java gui eventJava gui event
Java gui event
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART III
 
jquery examples
jquery examplesjquery examples
jquery examples
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 
25 awt
25 awt25 awt
25 awt
 
J query
J queryJ query
J query
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Jquery Example PPT
Jquery Example PPTJquery Example PPT
Jquery Example PPT
 

Similar a Chapter 11.5

event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDchessvashisth
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programmingSynapseindiappsdevelopment
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024kashyapneha2809
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024nehakumari0xf
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - eventsroxlu
 
01 Java Is Architecture Neutral
01 Java Is Architecture Neutral01 Java Is Architecture Neutral
01 Java Is Architecture Neutralrajuglobal
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 
File Handling
File HandlingFile Handling
File HandlingSohanur63
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 

Similar a Chapter 11.5 (20)

event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
What is Event
What is EventWhat is Event
What is Event
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKDPPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
PPThbkjn;l sdc a;s'jjN djkHBSDjhhIDoj LKD
 
Swing
SwingSwing
Swing
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
01 Java Is Architecture Neutral
01 Java Is Architecture Neutral01 Java Is Architecture Neutral
01 Java Is Architecture Neutral
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
09events
09events09events
09events
 
File Handling
File HandlingFile Handling
File Handling
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 

Más de sotlsoc

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 newsotlsoc
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 newsotlsoc
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 newsotlsoc
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 newsotlsoc
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1sotlsoc
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4sotlsoc
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3sotlsoc
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2sotlsoc
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0sotlsoc
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2sotlsoc
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1sotlsoc
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0sotlsoc
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0sotlsoc
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3sotlsoc
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4sotlsoc
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1sotlsoc
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1sotlsoc
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0sotlsoc
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2sotlsoc
 

Más de sotlsoc (20)

Chapter 2.0 new
Chapter 2.0 newChapter 2.0 new
Chapter 2.0 new
 
Chapter 1.0 new
Chapter 1.0 newChapter 1.0 new
Chapter 1.0 new
 
Chapter 3.0 new
Chapter 3.0 newChapter 3.0 new
Chapter 3.0 new
 
Chapter 6.5 new
Chapter 6.5 newChapter 6.5 new
Chapter 6.5 new
 
Chapter 11.1
Chapter 11.1Chapter 11.1
Chapter 11.1
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Chapter 11.4
Chapter 11.4Chapter 11.4
Chapter 11.4
 
Chapter 11.3
Chapter 11.3Chapter 11.3
Chapter 11.3
 
Chapter 11.2
Chapter 11.2Chapter 11.2
Chapter 11.2
 
Chapter 11.0
Chapter 11.0Chapter 11.0
Chapter 11.0
 
Chapter 10.2
Chapter 10.2Chapter 10.2
Chapter 10.2
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
Chapter 8.0
Chapter 8.0Chapter 8.0
Chapter 8.0
 
Chapter 10.0
Chapter 10.0Chapter 10.0
Chapter 10.0
 
Chapter 9.3
Chapter 9.3Chapter 9.3
Chapter 9.3
 
Chapter 9.4
Chapter 9.4Chapter 9.4
Chapter 9.4
 
Chapter 9.1
Chapter 9.1Chapter 9.1
Chapter 9.1
 
Chapter 8.1
Chapter 8.1Chapter 8.1
Chapter 8.1
 
Chapter 9.0
Chapter 9.0Chapter 9.0
Chapter 9.0
 
Chapter 9.2
Chapter 9.2Chapter 9.2
Chapter 9.2
 

Último

psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Último (20)

Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Chapter 11.5

  • 2. Event-Driven Programming  In event-driven programming, code is executed upon activation of events.  Ex: a button click, or mouse movement
  • 3. Events and Event Source  An event can be defined as a type of signal to the program that something has happened.  The event is generated by external user actions such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer.  The component on which an event is generated is called the source object.  Ex: source object: Button for a button-clicking action event.
  • 5. Event Information  An event object contains whatever properties are pertinent to the event.  You can identify the source object of the event using the getSource() instance method in the EventObject class.  The subclasses of EventObject deal with special types of events, such as button actions, window events, component events, mouse movements, and keystrokes.
  • 6. Selected User Actions Source Event Type User Action Object Generated Click a button JButton ActionEvent Click a check box JCheckBox ItemEvent, ActionEvent Click a radio button JRadioButton ItemEvent, ActionEvent Press Enter on a text field JTextField ActionEvent Select a new item JComboBox ItemEvent, ActionEvent Select item(s) JList ListSelectionEvent Select a menu item JMenuItem ActionEvent Window opened, closed, etc. Window WindowEvent Mouse pressed, released, etc. Component MouseEvent Key released, pressed, etc. Component KeyEvent In detail, refer to Table 16.1 pg 559
  • 7. Listeners, Registrations, and Handling Events  Java uses a delegation-based model for event handling  An external user action on a source object triggers an event  An object interested in the event receives the event  The latter object is called a listener
  • 8. Listeners, Registrations, and Handling Events  Two things are needed for an object to be a listener for an event on a source object: 1. The listener object’s class must implement the corresponding event-listener interface. Java provides a listener interface for every type of GUI event. The listener interface is usually named XListener for XEvent , Ex: Listener interface for ActionEvent is ActionListener each listener for ActionEvent should implement the ActionListener interface
  • 9. Selected Event Handlers Event Class Listener Interface Listener Methods (Handlers) ActionEvent ActionListener actionPerformed(ActionEvent) ItemEvent ItemListener itemStateChanged(ItemEvent) WindowEvent WindowListener windowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ContainerEvent ContainerListener componentAdded(ContainerEvent) componentRemoved(ContainerEvent) MouseEvent MouseListener mousePressed(MouseEvent) mouseReleased(MouseEvent) mouseClicked(MouseEvent) mouseExited(MouseEvent) mouseEntered(MouseEvent) KeyEvent KeyListener keyPressed(KeyEvent) keyReleased(KeyEvent) keyTypeed(KeyEvent) In detail, refer to Table 16.2 pg 561
  • 10. Listeners, Registrations, and Handling Events  Two things are needed for an object to be a listener for an event on a source object: 2. The listener object must be registered by the source object. Registration methods are dependent on the event type Ex: For ActionEvent, the method is addActionListener In general: the method is named addXListener for Xevent
  • 11. Listeners, Registrations, and Handling Events  For each event, the source object maintains a list of listeners and notifies all the registered listeners by invoking the handler on the listener object to respond to the event.
  • 12. The Delegation Model: Example source: JButton +addActionListener(ActionListener listener) listener: ListenerClass ActionListener +actionPerformed(ActionEvent event) Register by invoking source.addActionListener(listener); ListenerClass listener = new ListenerClass(); JButton jbt = new JButton("OK"); jbt.addActionListener(listener); class ListenerClass implements ActionListener { public void actionPerformed(ActionEvent e) { S.o.p (“Button “+ e.getActionCommand()+ “ is clicked.”);}
  • 13. Example:  Class must implement the listener interface (ex: ActionListener). and method to handle the event (ex: actionPerformed())  The listener object must register with source object (ex: button)  Done by invoking the addActionListener in the button object  Ex:: public class TestActionButton extends JFrame implements ActionListener { JButton btn = new JButton(“Click Here"); btn.addActionListener(this); public void actionPerformed(ActionEvent e) { } } Registration method (XEvent  addXListener)
  • 14. ActionEvent  The components that involved with this event are:  JButton – clicka a button  JMenuItem – select a menu item  JTextField – press return on a text field  Event listener interface – ActionListener  Event handler method public void actionPerformed(ActionEvent e)
  • 15. java.awt.event.ActionEvent java.awt.event.ActionEvent +getActionCommand(): String +getModifier(): int +getWhen(): long Returns the command string associated with this action. For a button, its text is the command string. Returns the modifier keys held down during this action event. Returns the timestamp when this event occurred. The time is the number of milliseconds since January 1, 1970, 00:00:00 GMT. java.util.EventObject +getSource(): Object Returns the object on which the event initially occurred. java.awt.event.AWTEvent
  • 16. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiActionButang extends JFrame implements ActionListener { JButton btg = new JButton("Sila Klik"); JTextField txtField = new JTextField(10); JLabel lbl = new JLabel(" "); public UjiActionButang() { super("Melaksanakan ActionEvent"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); bekas.add(btg); btg.addActionListener(this); bekas.add(txtField); bekas.add(lbl); setSize(300,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { lbl.setText("Terima Kasih"); txtField.setText("Sama-sama"); } } public static void main(String[] arg) { UjiActionButang teks = new UjiActionButang(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } To display label, and text on text field when button is clicked
  • 17. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionTextField extends JFrame implements ActionListener { JLabel lbl = new JLabel("Nama "); JTextField txtField = new JTextField(10); public ActionTextField() { super("Melaksanakan ActionEvent- TextField"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); bekas.add(lbl); bekas.add(txtField); txtField.addActionListener(this); setSize(300,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource()== txtField) { JOptionPane.showMessageDialog(this,"Nama anda = "+ txtField.getText(),"Pemberitahuan", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { ActionTextField teks = new ActionTextField(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } To display name from text field on dialog box when pressing <enter> on text field
  • 18. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class ActionButton extends JFrame implements ActionListener { JLabel lbl = new JLabel("Nama "); JTextField txtField = new JTextField(10); JButton btg = new JButton("Klik"); JTextArea txtArea = new JTextArea("Ali",10,6); public ActionButton() { super("Melaksanakan ActionEvent- Butang"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); bekas.add(lbl); bekas.add(txtField); bekas.add(btg); bekas.add(txtArea); btg.addActionListener(this); setSize(300,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { txtArea.append("n"+txtField.getText()); } } public static void main(String[] arg) { ActionButton teks = new ActionButton(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } To append text on text area when button is clicked
  • 19. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiActionMenu extends JFrame implements ActionListener { JFrame frame = new JFrame(); JMenuBar jmb = new JMenuBar(); JMenu fileMenu = new JMenu("File"); JMenuItem baru= new JMenuItem("New"); JMenuItem buka= new JMenuItem("Open"); public UjiActionMenu() { frame.setTitle("membuat menu"); baru.addActionListener(this); fileMenu.add(baru); fileMenu.add(buka); jmb.add(fileMenu); frame.setJMenuBar(jmb); frame.setSize(400,200); frame.setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == baru) JOptionPane.showMessageDialog(this,"Anda telah memilih menu New", "Pemberitahuan",JOptionPane.INFORMATION_MESSAGE); } public static void main(String[] arg) { UjiActionMenu teks = new UjiActionMenu(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } Display dialog box when menu item is clicked
  • 20. comboBox  Generate two types of event  ActionEvent  ItemEvent  For the case of ItemEvent:  Event listener Interface : ItemListener  Event handler method  public void ItemStateChanged(ItemEvent e) ○ When select an item in the combo box  Index for the first item in the combo box will be started by 0..n-1.
  • 21. comboBox javax.swing.JComboBox +JComboBox() +JComboBox(items: Object[]) +addItem(item: Object): void +getItemAt(index: int): Object +getItemCount(): int +getSelectedIndex(): int +setSelectedIndex(index: int): void +getSelectedItem(): Object +setSelectedItem(item: Object): void +removeItem(anObject: Object): void +removeItemAt(anIndex: int): void +removeAllItems(): void Creates a default empty combo box. Creates a combo box that contains the elements in the specified array. Adds an item to the combo box. Returns the item at the specified index. Returns the number of items in the combo box. Returns the index of the selected item. Sets the selected index in the combo box. Returns the selected item. Sets the selected item in the combo box. Removes an item from the item list. Removes the item at the specified index in the combo box. Removes all items in the combo box. javax.swing.JComponent
  • 22. Using the itemStateChanged Handler public void itemStateChanged(ItemEvent e) { // Make sure the source is a combo box if (e.getSource() instanceof JComboBox) String s = (String)e.getItem(); } When a new item is selected, itemStateChanged() for ItemEvent is invoked .
  • 23. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiItemEventComboBox extends JFrame implements ItemListener { JComboBox jcb = new JComboBox(); public UjiItemEventComboBox() { super("Membuat combobox"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); jcb.addItem("wira"); jcb.addItem("waja"); jcb.addItemListener(this); bekas.add(jcb); setSize(400,200); setVisible(true); } public void itemStateChanged(ItemEvent e) { if (e.getSource()== jcb) { if (e.getStateChange() == ItemEvent.SELECTED) { String s1 = (String) jcb.getSelectedItem(); JOptionPane.showMessageDialog(this,"Anda telah memilih "+s1,"Maklumat", JOptionPane.INFORMATION_MESSAGE); } } } ItemEvent: Display item on dialog box when item in Combo Box is selected
  • 24. public static void main(String[] arg) { UjiItemEventComboBox teks = new UjiItemEventComboBox(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 25. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiActionEventComboBox extends JFrame implements ActionListener { JComboBox jcb = new JComboBox(); JButton btg = new JButton("Klik"); public UjiActionEventComboBox() { super("Membuat combobox"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); jcb.addItem("wira"); jcb.addItem("waja"); btg.addActionListener(this); bekas.add(jcb); bekas.add(btg); setSize(400,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { String s1 = (String)jcb.getSelectedItem(); JOptionPane.showMessageDialog(this,"Anda telah memilih "+s1,“ Maklumat",JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { UjiActionEventComboBox teks = new UjiActionEventComboBox(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ActionEvent: Display selected item on dialog box when a button is clicked
  • 26. ListSelectionEvent - JList  Generates javax.swing.event.ListSelectionEvent to notify the listeners of the slections  Event listener interface : ListSelectionListener  Event handler interface:  public void valueChanged(ListSelectionEvent e) ○ When select item(s) in JList  Index for the first item in the JList will be started by 0..n-1.
  • 27. ListSelectionEvent - JList javax.swing.JList +JList() +JList(items: Object[]) +getSelectedIndex(): int +setSelectedIndex(index: int): void +getSelectedIndices(): int[] +setSelectedIndices(indices: int[]): void +getSelectedValue(): Object +getSelectedValues(): Object[] +getVisibleRowCount(): int +setVisibleRowCount(count: int): void +getSelectionBackground(): Color +setSelectionBackground(c: Color): void +getSelectionForeground(): Color +setSelectionForeground(c: Color): void +getSelectionMode(): int +setSelectionMode(selectionMode: int): Creates a default empty list. Creates a list that contains the elements in the specified array. Returns the index of the first selected item. Selects the cell at the specified index. Returns an array of all of the selected indices in increasing order. Selects the cells at the specified indices. Returns the first selected item in the list. Returns an array of the values for the selected cells in increasing index order. Returns the number of visible rows displayed without a scrollbar. (default: 8) Sets the preferred number of visible rows displayed without a scrollbar. Returns the background color of the selected cells. Sets the background color of the selected cells. Returns the foreground color of the selected cells. Sets the foreground color of the selected cells. Returns the selection mode for the list. Sets the selection mode for the list. javax.swing.JComponent
  • 28. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class UjiSelectionEventList extends JFrame implements ListSelectionListener { String[] jenisWarna= {"merah","hijau","kuning","jingga","biru"}; JList senaraiWarna; public UjiSelectionEventList() { super("Membuat JList"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); senaraiWarna = new JList(jenisWarna); senaraiWarna.addListSelectionListener(this); senaraiWarna.setVisibleRowCount(3); senaraiWarna.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); bekas.add(new JScrollPane(senaraiWarna)); setSize(300,200); setVisible(true); } public void valueChanged(ListSelectionEvent e) { if (e.getSource()== senaraiWarna) { String s1 = (String)senaraiWarna.getSelectedValue(); JOptionPane.showMessageDialog(this,s1,"maklumat", JOptionPane.INFORMATION_MESSAGE); } } SelectionEvent: Display selected item when item in JList is clicked
  • 29. public static void main(String[] arg) { UjiSelectionEventList list = new UjiSelectionEventList(); list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 30. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class DataListEventt extends JFrame implements ActionListener { String[] jenisWarna= {"merah","hijau","kuning","jingga","biru"}; JList senaraiWarna; JButton btg = new JButton("Klik"); public DataListEventt() { super("Membuat JList"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); senaraiWarna = new JList(jenisWarna); senaraiWarna.setVisibleRowCount(3); senaraiWarna.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); bekas.add(new JScrollPane(senaraiWarna)); bekas.add(btg); btg.addActionListener(this); setSize(300,200); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { String s1 = (String)senaraiWarna.getSelectedValue(); JOptionPane.showMessageDialog(this,s1,"maklumat", JOptionPane.INFORMATION_MESSAGE); } } ActionEvent: Display selected item in JList when a button is clicked
  • 31. public static void main(String[] arg) { DataListEventt list = new DataListEventt(); list.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 32. ItemEvent - RadioButton  Event Listener Interface : ItemListener  Event handler method :  public void ItemStateChanged(ItemEvent e) ○ Click a radio button
  • 33. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiItemListenerRadioButton extends JFrame implements ItemListener { JRadioButton rdButton1,rdButton2; public UjiItemListenerRadioButton() { super("Membuat RadioButton"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); rdButton1 = new JRadioButton("wira"); rdButton2 = new JRadioButton("Waja",true); rdButton1.addItemListener(this); rdButton2.addItemListener(this); bekas.add(rdButton1); bekas.add(rdButton2); ButtonGroup butang = new ButtonGroup(); butang.add(rdButton1); butang.add(rdButton2); setSize(400,200); setVisible(true); } ItemEvent: Display item on dialog box when a radio button is clicked
  • 34. public void itemStateChanged(ItemEvent e) { if (e.getSource()== rdButton1) { if (e.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(this,"kereta wira","Maklumat", JOptionPane.INFORMATION_MESSAGE); } if (e.getSource()== rdButton2) { if (e.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(this,"kereta waja","Maklumat", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { UjiItemListenerRadioButton teks = new UjiItemListenerRadioButton(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 35. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DataRadioButton extends JFrame implements ActionListener { JRadioButton rdButton1,rdButton2; JButton btg = new JButton("Klik"); String kereta; public DataRadioButton() { super("Membuat RadioButton"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); rdButton1 = new JRadioButton("wira"); rdButton2 = new JRadioButton("Waja",true); bekas.add(rdButton1); bekas.add(rdButton2); bekas.add(btg); btg.addActionListener(this); ButtonGroup butang = new ButtonGroup(); butang.add(rdButton1); butang.add(rdButton2); setSize(400,200); setVisible(true); } ActionEvent: Display selected item from radio button when a button is clicked
  • 36. public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { if (rdButton1.isSelected()) kereta = "Wira"; if (rdButton2.isSelected()) kereta = "Waja"; JOptionPane.showMessageDialog(this,kereta,"maklumat", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { DataRadioButton teks = new DataRadioButton(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 37. ItemEvent - CheckBox  Event Listener Interface : ItemListener  Event handler metod :  public void ItemStateChanged(ItemEvent e) ○ Click a check box
  • 38. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class UjiItemEventCheckBox extends JFrame implements ItemListener { JCheckBox chkBox1,chkBox2; public UjiItemEventCheckBox() { super("Membuat CheckBox"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); chkBox1 = new JCheckBox("Wira"); chkBox1.addItemListener(this); chkBox2 = new JCheckBox("Waja"); bekas.add(chkBox1); bekas.add(chkBox2); setSize(400,200); setVisible(true); } public void itemStateChanged(ItemEvent e) { if (e.getSource()== chkBox1) { if (e.getStateChange() == ItemEvent.SELECTED) JOptionPane.showMessageDialog(this,"Di tanda","Maklumat", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { UjiItemEventCheckBox teks = new UjiItemEventCheckBox(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } ItemEvent: Display a message when clicking a check box
  • 39. import java.awt.*; import javax.swing.*; import java.awt.event.*; public class DataCheckBox extends JFrame implements ActionListener { JCheckBox chkBox1,chkBox2; JButton btg = new JButton("Klik"); String kereta; public DataCheckBox() { super("Membuat CheckBox"); Container bekas = getContentPane(); bekas.setLayout(new FlowLayout()); chkBox1 = new JCheckBox("Wira"); chkBox2 = new JCheckBox("Waja"); bekas.add(chkBox1); bekas.add(chkBox2); bekas.add(btg); btg.addActionListener(this); setSize(400,200); setVisible(true); } ActionEvent: Display selected item from check box when button is clicked
  • 40. public void actionPerformed(ActionEvent e) { if (e.getSource()== btg) { if (chkBox1.isSelected()) kereta = "Wira"; if (chkBox2.isSelected()) kereta = "Waja"; JOptionPane.showMessageDialog(this,kereta,"maklumat", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] arg) { DataCheckBox teks = new DataCheckBox(); teks.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }