SlideShare a Scribd company logo
Event handling in Java(part 2)
LISTENER INTERFACE METHODS
ActionListener actionPerformed()
AdjustmentListener adjustmentValueChanged()
ComponentListener componentResized()
componentMoved()
componentShown()
componentHidden()
ContainerListener componentAdded()
componentRemoved()
Event handling in Java(part 2)
ActionEvent
An ActionEvent is generated when a button is pressed, a
list item is double-clicked, or a menu item is selected
 public String getActionCommand()
Returns the command string associated with this
action.
 int getModifiers()
Returns the modifier keys held down during this action
event.
 String paramString()
Returns a parameter string identifying this action event
Four integer constants
 public static final int ALT_MASK
The alt modifier. An indicator that the alt key was held down during the event.
 public static final int SHIFT_MASK
The shift modifier. An indicator that the shift key was held down during the event.
 public static final int CTRL_MASK
The control modifier. An indicator that the control key was held down during the
event.
 public static final int META_MASK
The meta modifier. An indicator that the meta key was held down during the event.
ActionListener Interface
This interface defines the actionPerformed() method
that is invoked when an action event occurs.
Its general form is shown here:
void actionPerformed(ActionEvent ae)
Event handling in Java(part 2)
OUTPUT
Event handling in Java(part 2)
 int getAdjustmentType( )
The amount of the adjustment can be obtained from the
getValue( ) method
 int getValue( )
For example, when a scroll bar is manipulated, this method returns
the value represented by that change.
Event handling in Java(part 2)
AdjustmentListener Interface
This interface defines the adjustmentValueChanged( )
method that is invoked when an adjustment event occurs
public class AdjustmentListenerTest implements AdjustmentListener
{
Scrollbar sbar1 = new Scrollbar();
sbar1.addAdjustmentListener(this);
add(sbar1, "West");
Scrollbar sbar2=new Scrollbar();
add(sbar2,"East");
}
public void adjustmentValueChanged(AdjustmentEvent
AdjEvt)
{
System.out.println(AdjEvt.getValue());
}
}
Event handling in Java(part 2)
ComponentEvent class
Indicates that a component moved, changed size, or
changed visibility.
This class has following constants.
 public static final int COMPONENT_MOVED
This event indicates that the component's position
changed.
 public static final int COMPONENT_RESIZED
This event indicates that the component's size changed.
 public static final int COMPONENT_SHOWN
This event indicates that the component was made visible.
 public static final int COMPONENT_HIDDEN
This event indicates that the component was become invisible.
ComponentListener interface
The listener interface for receiving component events.
 void componentResized(ComponentEvent e)
Invoked when the component's size changes.
 void componentMoved(ComponentEvent e)
Invoked when the component's position changes
 void componentShown(ComponentEvent e)
Invoked when the component has been made visible.
 void componentHidden(ComponentEvent e)
Invoked when the component has been made invisible.
import java.awt.*;
import java.awt.event;
public class ComponentEventExample1
{
public static void main(String[] args)
{
Frame frame = new Frame("ComponentEventExample");
TextArea txtArea = new TextArea();
Checkbox checkbox1 = new Checkbox("Checkbox 1");
Checkbox checkbox2 = new Checkbox("Checkbox 2");
frame.add(txtArea, BorderLayout.CENTER);
frame.add(checkbox1, BorderLayout.NORTH);
frame.add(checkbox2, BorderLayout.SOUTH);
frame.setVisible(true);
ComponentListener componentListener = new MyCompList();
frame.addComponentListener(componentListener);
}
}
class MyCompList implements ComponentListener
{
public void componentShown(ComponentEvent evt)
{
System.out.println("componentShown");
}
public void componentHidden(ComponentEvent evt)
{
System.out.println("componentHidden");
}
public void componentMoved(ComponentEvent evt)
{
System.out.println("componentMoved");
}
public void componentResized(ComponentEvent evt)
{
System.out.println("componentResized");
} }
Event handling in Java(part 2)
ContainerEvent class
Indicates that a container's contents changed because a component
was added or removed
This class has following constants.
 public static final int COMPONENT_ADDED
This event indicates that a component was added to the
container.
 public static final int COMPONENT_REMOVED
This event indicates that a component was removed from the
container
ContainerListener interface
The listener interface for receiving container events
• void componentAdded(ContainerEvent e)
Invoked when a component has been added to the container.
• void componentRemoved (ContainerEvent e)
Invoked when a component has been removed from the container.
Instance Methods
• getChild()
public Component getChild()
Returns the component that is being added or removed.
• getContainer()
public Container getContainer()
Returns the container that fired this event.
Event handling in Java(part 2)
Event handling in Java(part 2)
Event handling in Java(part 2)

More Related Content

PPTX
Event handling in Java(part 1)
PDF
Unit-3 event handling
PPT
Java eventhandling
PPT
Java Event Handling
PPTX
tL20 event handling
PPTX
Event handling
PPTX
Event Handling in java
Event handling in Java(part 1)
Unit-3 event handling
Java eventhandling
Java Event Handling
tL20 event handling
Event handling
Event Handling in java

What's hot (19)

PPTX
Advance Java Programming(CM5I) Event handling
PDF
Ajp notes-chapter-03
PPTX
Event handling
PPT
Event handling63
PPTX
Chapter 11.5
PPT
Java gui event
PDF
12 High Level UI Event Handling
PDF
JAVA GUI PART III
DOCX
Lecture8 oopj
PPT
Graphical User Interface (GUI) - 2
PPTX
What is Event
PPTX
Androd Listeners
DOCX
Adding a action listener to button
PPTX
Lesson 07 Actions and Commands in WPF
PPTX
Android development session 2 - intent and activity
PPT
25 awt
DOC
Calculadora
PPT
Java: GUI
PDF
Intents in Android
Advance Java Programming(CM5I) Event handling
Ajp notes-chapter-03
Event handling
Event handling63
Chapter 11.5
Java gui event
12 High Level UI Event Handling
JAVA GUI PART III
Lecture8 oopj
Graphical User Interface (GUI) - 2
What is Event
Androd Listeners
Adding a action listener to button
Lesson 07 Actions and Commands in WPF
Android development session 2 - intent and activity
25 awt
Calculadora
Java: GUI
Intents in Android
Ad

Similar to Event handling in Java(part 2) (20)

PPTX
event-handling.pptx
PPT
event handling new.ppt
PPT
PPT
engineeringdsgtnotesofunitfivesnists.ppt
PPT
Unit 5.133333333333333333333333333333333.ppt
PPTX
PPT
Java awt
PPTX
JAVA AWT
PPTX
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
PPTX
EventHandling in object oriented programming
PPTX
Event Handling in JAVA
PPTX
Android Event and IntentAndroid Event and Intent
PDF
Swing
DOCX
Dr Jammi Ashok - Introduction to Java Material (OOPs)
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPT
01 Java Is Architecture Neutral
PPT
Synapseindia dotnet development chapter 14 event-driven programming
KEY
openFrameworks 007 - events
PDF
2011 07-hiyoko
event-handling.pptx
event handling new.ppt
engineeringdsgtnotesofunitfivesnists.ppt
Unit 5.133333333333333333333333333333333.ppt
Java awt
JAVA AWT
JAVA UNIT 5.pptx jejsjdkkdkdkjjndjfjfkfjfnfn
EventHandling in object oriented programming
Event Handling in JAVA
Android Event and IntentAndroid Event and Intent
Swing
Dr Jammi Ashok - Introduction to Java Material (OOPs)
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
01 Java Is Architecture Neutral
Synapseindia dotnet development chapter 14 event-driven programming
openFrameworks 007 - events
2011 07-hiyoko
Ad

Recently uploaded (20)

PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Lesson notes of climatology university.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharma ospi slides which help in ospi learning
PPH.pptx obstetrics and gynecology in nursing
O5-L3 Freight Transport Ops (International) V1.pdf
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Lesson notes of climatology university.
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
TR - Agricultural Crops Production NC III.pdf
Anesthesia in Laparoscopic Surgery in India
Microbial disease of the cardiovascular and lymphatic systems
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Module 4: Burden of Disease Tutorial Slides S2 2025
Renaissance Architecture: A Journey from Faith to Humanism
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Cell Types and Its function , kingdom of life
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...

Event handling in Java(part 2)

  • 2. LISTENER INTERFACE METHODS ActionListener actionPerformed() AdjustmentListener adjustmentValueChanged() ComponentListener componentResized() componentMoved() componentShown() componentHidden() ContainerListener componentAdded() componentRemoved()
  • 4. ActionEvent An ActionEvent is generated when a button is pressed, a list item is double-clicked, or a menu item is selected
  • 5.  public String getActionCommand() Returns the command string associated with this action.  int getModifiers() Returns the modifier keys held down during this action event.  String paramString() Returns a parameter string identifying this action event
  • 6. Four integer constants  public static final int ALT_MASK The alt modifier. An indicator that the alt key was held down during the event.  public static final int SHIFT_MASK The shift modifier. An indicator that the shift key was held down during the event.  public static final int CTRL_MASK The control modifier. An indicator that the control key was held down during the event.  public static final int META_MASK The meta modifier. An indicator that the meta key was held down during the event.
  • 7. ActionListener Interface This interface defines the actionPerformed() method that is invoked when an action event occurs. Its general form is shown here: void actionPerformed(ActionEvent ae)
  • 11.  int getAdjustmentType( ) The amount of the adjustment can be obtained from the getValue( ) method  int getValue( ) For example, when a scroll bar is manipulated, this method returns the value represented by that change.
  • 13. AdjustmentListener Interface This interface defines the adjustmentValueChanged( ) method that is invoked when an adjustment event occurs
  • 14. public class AdjustmentListenerTest implements AdjustmentListener { Scrollbar sbar1 = new Scrollbar(); sbar1.addAdjustmentListener(this); add(sbar1, "West"); Scrollbar sbar2=new Scrollbar(); add(sbar2,"East"); } public void adjustmentValueChanged(AdjustmentEvent AdjEvt) { System.out.println(AdjEvt.getValue()); } }
  • 16. ComponentEvent class Indicates that a component moved, changed size, or changed visibility. This class has following constants.  public static final int COMPONENT_MOVED This event indicates that the component's position changed.  public static final int COMPONENT_RESIZED This event indicates that the component's size changed.  public static final int COMPONENT_SHOWN This event indicates that the component was made visible.  public static final int COMPONENT_HIDDEN This event indicates that the component was become invisible.
  • 17. ComponentListener interface The listener interface for receiving component events.  void componentResized(ComponentEvent e) Invoked when the component's size changes.  void componentMoved(ComponentEvent e) Invoked when the component's position changes  void componentShown(ComponentEvent e) Invoked when the component has been made visible.  void componentHidden(ComponentEvent e) Invoked when the component has been made invisible.
  • 18. import java.awt.*; import java.awt.event; public class ComponentEventExample1 { public static void main(String[] args) { Frame frame = new Frame("ComponentEventExample"); TextArea txtArea = new TextArea(); Checkbox checkbox1 = new Checkbox("Checkbox 1"); Checkbox checkbox2 = new Checkbox("Checkbox 2"); frame.add(txtArea, BorderLayout.CENTER); frame.add(checkbox1, BorderLayout.NORTH); frame.add(checkbox2, BorderLayout.SOUTH); frame.setVisible(true); ComponentListener componentListener = new MyCompList(); frame.addComponentListener(componentListener); } }
  • 19. class MyCompList implements ComponentListener { public void componentShown(ComponentEvent evt) { System.out.println("componentShown"); } public void componentHidden(ComponentEvent evt) { System.out.println("componentHidden"); } public void componentMoved(ComponentEvent evt) { System.out.println("componentMoved"); } public void componentResized(ComponentEvent evt) { System.out.println("componentResized"); } }
  • 21. ContainerEvent class Indicates that a container's contents changed because a component was added or removed This class has following constants.  public static final int COMPONENT_ADDED This event indicates that a component was added to the container.  public static final int COMPONENT_REMOVED This event indicates that a component was removed from the container
  • 22. ContainerListener interface The listener interface for receiving container events • void componentAdded(ContainerEvent e) Invoked when a component has been added to the container. • void componentRemoved (ContainerEvent e) Invoked when a component has been removed from the container.
  • 23. Instance Methods • getChild() public Component getChild() Returns the component that is being added or removed. • getContainer() public Container getContainer() Returns the container that fired this event.