This document discusses mouse event handling in Java. It introduces event-driven programming and shows how to implement listeners to respond to mouse events like clicks and movements. It provides templates for creating mouse click listeners and mouse motion listeners, and explains how to get the mouse location from event arguments. Methods like addMouseListener and addMouseMotionListener are used to register listeners with the event manager.
This document discusses mouse event handling in Java. It introduces event-driven programming and shows how to implement listeners to respond to mouse events like clicks and movements. It provides templates for creating mouse click listeners and mouse motion listeners, and explains how to get the mouse location from event arguments. Methods like addMouseListener and addMouseMotionListener are used to register listeners with the event manager.
This document discusses event handling in Java. It covers using the delegation event model, handling keyboard and mouse events, and using adapter classes. Key points covered include implementing the appropriate interface for the event desired, registering the listener, and providing empty implementations in adapter classes to simplify creating event handlers. Examples are provided to demonstrate handling keyboard and mouse events.
This document discusses different strategies for handling mouse and keyboard events in Java programs. It describes registering listener objects to handle events from GUI components and defines the standard AWT listener types. The strategies covered include using separate listener classes, implementing listener interfaces, and defining named and anonymous inner classes. Examples are provided to illustrate drawing on mouse clicks and key presses.
The document discusses Java event handling and the delegation event model. It describes key concepts like events, sources that generate events, and listeners that handle events. It provides examples of registering components as listeners and implementing listener interfaces. The delegation event model joins sources, listeners, and events by notifying listeners when sources generate events.
1. An event describes a change in state of an object and is generated from user interaction with GUI components like buttons, mouse movement, keyboard entry etc.
2. There are two types of events - foreground events from direct user interaction and background events from system processes.
3. Event handling is the mechanism that controls how programs respond to events using event handlers (listener objects) that contain code to execute when an event occurs. The delegation event model defines how events are generated and handled in Java.
This document discusses different strategies for handling mouse and keyboard events in Java applications, including using separate listener classes, implementing listener interfaces, and defining inner listener classes. It provides examples of drawing shapes in response to mouse clicks and keyboard input. It also summarizes the standard AWT listener types and their corresponding methods.
This document discusses GUI event handling in Java. It introduces the delegation event model where event sources generate event objects that are passed to registered event listeners. It covers common event classes and listener interfaces like ActionListener, MouseListener, and WindowListener. It provides examples of handling mouse and window events using different approaches like inner classes and anonymous inner classes.
The document discusses building touchscreen interfaces for mobile devices using Flash Lite. It covers touchscreen technologies, gestural interfaces, coherence with platforms, and considerations for Flash Lite and touch including supporting touch events, building dynamic lists, and performance. Example code is provided to attach listeners for touch events, define the scrollable area, and implement scrolling and selection.
- The document discusses event handling in Java GUI programs.
- It explains the Java AWT event delegation model where event sources generate events that are passed to registered listener objects.
- An example program is shown where a button generates an ActionEvent when clicked, which is handled by a listener class that implements the ActionListener interface.
- The AWT event hierarchy and common event types like KeyEvents and MouseEvents are described. Individual events provide information about user input.
- Adapter classes are mentioned which provide default empty implementations of listener interfaces to simplify coding listeners.
Applet Basics,
Applet Organization and Essential Elements,
The Applet Architecture,
A Complete Applet Skeleton,
Applet Initialization and Termination,
Requesting Repainting
The update() Method,
Using the Status Window
Passing parameters to Applets
The Applet Class
Event Handling The Delegation Event Model
Events,
Using the Delegation Event Model,
More Java Keywords.
The document discusses object oriented programming concepts like events, event sources, event classes, event listeners, and the delegation event model. It describes how events like mouse clicks and keyboard presses are handled in Java. It provides details on common event classes like MouseEvent and KeyEvent. It also discusses components of the AWT class hierarchy like labels, buttons, text fields, and scrollbars, and how to handle user interface events with them.
This document provides information about graphical user interfaces (GUIs) in Java. It discusses the AWT class hierarchy including important classes like Component, Container, Frame, and Panel. It covers event handling using the delegation event model and describes common listener interfaces. It also summarizes common GUI components like labels, buttons, text fields, and menus. Layout managers and concepts of applets versus applications are briefly mentioned.
This document discusses the evolution of graphical user interface (GUI) capabilities in the Java programming language. It describes the Abstract Window Toolkit (AWT) introduced in JDK 1.0, which provided basic cross-platform GUI functionality but had limitations. JDK 1.1 improved on AWT with an event delegation model. JDK 1.2 introduced Swing, a richer GUI library that better integrated with native operating system look and feels. Swing components are lightweight compared to heavyweight AWT components. The document also covers GUI component classes, layout managers, menus, labels and event handling in Java GUI programming.
The document discusses the top 3 SWT exceptions:
1. Out of Handles Error which occurs when there are no more OS handles for requested resources. Proper disposal of resources is needed to avoid this.
2. Invalid Thread Access Exception which occurs when accessing widgets from non-UI threads, in violation of SWT's single-threaded model. AsyncExec and SyncExec should be used.
3. Widget is Disposed Exception which occurs when accessing a widget that has already been disposed. Widget disposal must be checked before access.
How to use Listener Class in Flutter.pptxRubenGray1
This article is about the Listener class in Flutter. You are going to learn how to use or implement the Listener class in Flutter app development. Read the article for a complete guide.
The document discusses various event handling classes in Java including ActionEvent, KeyEvent, MouseEvent, MouseMotionEvent, FocusEvent, WindowEvent, and ItemEvent. It provides examples of how to use each event class by implementing the appropriate listener interface and defining event handling methods. Key points covered include common event handling terms like event, event source, and event listener. It also summarizes the typical methods provided by each event class.
This document discusses event handling and the Abstract Window Toolkit (AWT) in Java. It contains questions and answers on topics like the delegation event model, common event classes and listeners in Java, how to write code to handle mouse and keyboard events, and different AWT components like buttons, checkboxes, lists, frames, and scrollbars. Example code is provided to demonstrate how to use event listeners and handle mouse events in a Java program using the AWT.
This document provides an overview of event handling in Java. It discusses the delegation event model where a source generates an event and sends it to one or more listeners. It describes event sources, event listeners, common event classes like ActionEvent, MouseEvent, and KeyEvent. It explains the roles of sources that generate events, listeners that receive event notifications, and event classes that represent specific types of events.
Developing social simulations with UbikSimEmilio Serrano
UbikSim is a framework for developing social simulations in Java that emphasizes realistic indoor environments, person behaviors, and evaluation of ubiquitous computing systems. It uses third party libraries like SweetHome3D and MASON. The document provides instructions on creating environments and simulations in UbikSim, including modeling person behaviors using hierarchical state machines. It also discusses techniques for testing, debugging, running batch experiments and logging data in UbikSim simulations.
This presentation was provided by Bill Kasdorf of Kasdorf & Associates LLC and Publishing Technology Partners, during the fifth session of the NISO training series "Accessibility Essentials." Session Five: A Standards Seminar, was held May 1, 2025.
This document discusses GUI event handling in Java. It introduces the delegation event model where event sources generate event objects that are passed to registered event listeners. It covers common event classes and listener interfaces like ActionListener, MouseListener, and WindowListener. It provides examples of handling mouse and window events using different approaches like inner classes and anonymous inner classes.
The document discusses building touchscreen interfaces for mobile devices using Flash Lite. It covers touchscreen technologies, gestural interfaces, coherence with platforms, and considerations for Flash Lite and touch including supporting touch events, building dynamic lists, and performance. Example code is provided to attach listeners for touch events, define the scrollable area, and implement scrolling and selection.
- The document discusses event handling in Java GUI programs.
- It explains the Java AWT event delegation model where event sources generate events that are passed to registered listener objects.
- An example program is shown where a button generates an ActionEvent when clicked, which is handled by a listener class that implements the ActionListener interface.
- The AWT event hierarchy and common event types like KeyEvents and MouseEvents are described. Individual events provide information about user input.
- Adapter classes are mentioned which provide default empty implementations of listener interfaces to simplify coding listeners.
Applet Basics,
Applet Organization and Essential Elements,
The Applet Architecture,
A Complete Applet Skeleton,
Applet Initialization and Termination,
Requesting Repainting
The update() Method,
Using the Status Window
Passing parameters to Applets
The Applet Class
Event Handling The Delegation Event Model
Events,
Using the Delegation Event Model,
More Java Keywords.
The document discusses object oriented programming concepts like events, event sources, event classes, event listeners, and the delegation event model. It describes how events like mouse clicks and keyboard presses are handled in Java. It provides details on common event classes like MouseEvent and KeyEvent. It also discusses components of the AWT class hierarchy like labels, buttons, text fields, and scrollbars, and how to handle user interface events with them.
This document provides information about graphical user interfaces (GUIs) in Java. It discusses the AWT class hierarchy including important classes like Component, Container, Frame, and Panel. It covers event handling using the delegation event model and describes common listener interfaces. It also summarizes common GUI components like labels, buttons, text fields, and menus. Layout managers and concepts of applets versus applications are briefly mentioned.
This document discusses the evolution of graphical user interface (GUI) capabilities in the Java programming language. It describes the Abstract Window Toolkit (AWT) introduced in JDK 1.0, which provided basic cross-platform GUI functionality but had limitations. JDK 1.1 improved on AWT with an event delegation model. JDK 1.2 introduced Swing, a richer GUI library that better integrated with native operating system look and feels. Swing components are lightweight compared to heavyweight AWT components. The document also covers GUI component classes, layout managers, menus, labels and event handling in Java GUI programming.
The document discusses the top 3 SWT exceptions:
1. Out of Handles Error which occurs when there are no more OS handles for requested resources. Proper disposal of resources is needed to avoid this.
2. Invalid Thread Access Exception which occurs when accessing widgets from non-UI threads, in violation of SWT's single-threaded model. AsyncExec and SyncExec should be used.
3. Widget is Disposed Exception which occurs when accessing a widget that has already been disposed. Widget disposal must be checked before access.
How to use Listener Class in Flutter.pptxRubenGray1
This article is about the Listener class in Flutter. You are going to learn how to use or implement the Listener class in Flutter app development. Read the article for a complete guide.
The document discusses various event handling classes in Java including ActionEvent, KeyEvent, MouseEvent, MouseMotionEvent, FocusEvent, WindowEvent, and ItemEvent. It provides examples of how to use each event class by implementing the appropriate listener interface and defining event handling methods. Key points covered include common event handling terms like event, event source, and event listener. It also summarizes the typical methods provided by each event class.
This document discusses event handling and the Abstract Window Toolkit (AWT) in Java. It contains questions and answers on topics like the delegation event model, common event classes and listeners in Java, how to write code to handle mouse and keyboard events, and different AWT components like buttons, checkboxes, lists, frames, and scrollbars. Example code is provided to demonstrate how to use event listeners and handle mouse events in a Java program using the AWT.
This document provides an overview of event handling in Java. It discusses the delegation event model where a source generates an event and sends it to one or more listeners. It describes event sources, event listeners, common event classes like ActionEvent, MouseEvent, and KeyEvent. It explains the roles of sources that generate events, listeners that receive event notifications, and event classes that represent specific types of events.
Developing social simulations with UbikSimEmilio Serrano
UbikSim is a framework for developing social simulations in Java that emphasizes realistic indoor environments, person behaviors, and evaluation of ubiquitous computing systems. It uses third party libraries like SweetHome3D and MASON. The document provides instructions on creating environments and simulations in UbikSim, including modeling person behaviors using hierarchical state machines. It also discusses techniques for testing, debugging, running batch experiments and logging data in UbikSim simulations.
This presentation was provided by Bill Kasdorf of Kasdorf & Associates LLC and Publishing Technology Partners, during the fifth session of the NISO training series "Accessibility Essentials." Session Five: A Standards Seminar, was held May 1, 2025.
Contact Lens:::: An Overview.pptx.: OptometryMushahidRaza8
A comprehensive guide for Optometry students: understanding in easy launguage of contact lens.
Don't forget to like,share and comments if you found it useful!.
A measles outbreak originating in West Texas has been linked to confirmed cases in New Mexico, with additional cases reported in Oklahoma and Kansas. The current case count is 795 from Texas, New Mexico, Oklahoma, and Kansas. 95 individuals have required hospitalization, and 3 deaths, 2 children in Texas and one adult in New Mexico. These fatalities mark the first measles-related deaths in the United States since 2015 and the first pediatric measles death since 2003.
The YSPH Virtual Medical Operations Center Briefs (VMOC) were created as a service-learning project by faculty and graduate students at the Yale School of Public Health in response to the 2010 Haiti Earthquake. Each year, the VMOC Briefs are produced by students enrolled in Environmental Health Science Course 581 - Public Health Emergencies: Disaster Planning and Response. These briefs compile diverse information sources – including status reports, maps, news articles, and web content– into a single, easily digestible document that can be widely shared and used interactively. Key features of this report include:
- Comprehensive Overview: Provides situation updates, maps, relevant news, and web resources.
- Accessibility: Designed for easy reading, wide distribution, and interactive use.
- Collaboration: The “unlocked" format enables other responders to share, copy, and adapt seamlessly. The students learn by doing, quickly discovering how and where to find critical information and presenting it in an easily understood manner.
How to Set warnings for invoicing specific customers in odooCeline George
Odoo 16 offers a powerful platform for managing sales documents and invoicing efficiently. One of its standout features is the ability to set warnings and block messages for specific customers during the invoicing process.
The *nervous system of insects* is a complex network of nerve cells (neurons) and supporting cells that process and transmit information. Here's an overview:
Structure
1. *Brain*: The insect brain is a complex structure that processes sensory information, controls behavior, and integrates information.
2. *Ventral nerve cord*: A chain of ganglia (nerve clusters) that runs along the insect's body, controlling movement and sensory processing.
3. *Peripheral nervous system*: Nerves that connect the central nervous system to sensory organs and muscles.
Functions
1. *Sensory processing*: Insects can detect and respond to various stimuli, such as light, sound, touch, taste, and smell.
2. *Motor control*: The nervous system controls movement, including walking, flying, and feeding.
3. *Behavioral responThe *nervous system of insects* is a complex network of nerve cells (neurons) and supporting cells that process and transmit information. Here's an overview:
Structure
1. *Brain*: The insect brain is a complex structure that processes sensory information, controls behavior, and integrates information.
2. *Ventral nerve cord*: A chain of ganglia (nerve clusters) that runs along the insect's body, controlling movement and sensory processing.
3. *Peripheral nervous system*: Nerves that connect the central nervous system to sensory organs and muscles.
Functions
1. *Sensory processing*: Insects can detect and respond to various stimuli, such as light, sound, touch, taste, and smell.
2. *Motor control*: The nervous system controls movement, including walking, flying, and feeding.
3. *Behavioral responses*: Insects can exhibit complex behaviors, such as mating, foraging, and social interactions.
Characteristics
1. *Decentralized*: Insect nervous systems have some autonomy in different body parts.
2. *Specialized*: Different parts of the nervous system are specialized for specific functions.
3. *Efficient*: Insect nervous systems are highly efficient, allowing for rapid processing and response to stimuli.
The insect nervous system is a remarkable example of evolutionary adaptation, enabling insects to thrive in diverse environments.
The insect nervous system is a remarkable example of evolutionary adaptation, enabling insects to thrive
GDGLSPGCOER - Git and GitHub Workshop.pptxazeenhodekar
This presentation covers the fundamentals of Git and version control in a practical, beginner-friendly way. Learn key commands, the Git data model, commit workflows, and how to collaborate effectively using Git — all explained with visuals, examples, and relatable humor.
"Basics of Heterocyclic Compounds and Their Naming Rules"rupalinirmalbpharm
This video is about heterocyclic compounds, which are chemical compounds with rings that include atoms like nitrogen, oxygen, or sulfur along with carbon. It covers:
Introduction – What heterocyclic compounds are.
Prefix for heteroatom – How to name the different non-carbon atoms in the ring.
Suffix for heterocyclic compounds – How to finish the name depending on the ring size and type.
Nomenclature rules – Simple rules for naming these compounds the right way.
Common rings – Examples of popular heterocyclic compounds used in real life.
Geography Sem II Unit 1C Correlation of Geography with other school subjectsProfDrShaikhImran
The correlation of school subjects refers to the interconnectedness and mutual reinforcement between different academic disciplines. This concept highlights how knowledge and skills in one subject can support, enhance, or overlap with learning in another. Recognizing these correlations helps in creating a more holistic and meaningful educational experience.
*Metamorphosis* is a biological process where an animal undergoes a dramatic transformation from a juvenile or larval stage to a adult stage, often involving significant changes in form and structure. This process is commonly seen in insects, amphibians, and some other animals.
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsesushreesangita003
what is pulse ?
Purpose
physiology and Regulation of pulse
Characteristics of pulse
factors affecting pulse
Sites of pulse
Alteration of pulse
for BSC Nursing 1st semester
for Gnm Nursing 1st year
Students .
vitalsign
Understanding P–N Junction Semiconductors: A Beginner’s GuideGS Virdi
Dive into the fundamentals of P–N junctions, the heart of every diode and semiconductor device. In this concise presentation, Dr. G.S. Virdi (Former Chief Scientist, CSIR-CEERI Pilani) covers:
What Is a P–N Junction? Learn how P-type and N-type materials join to create a diode.
Depletion Region & Biasing: See how forward and reverse bias shape the voltage–current behavior.
V–I Characteristics: Understand the curve that defines diode operation.
Real-World Uses: Discover common applications in rectifiers, signal clipping, and more.
Ideal for electronics students, hobbyists, and engineers seeking a clear, practical introduction to P–N junction semiconductors.
Understanding P–N Junction Semiconductors: A Beginner’s GuideGS Virdi
Ad
java - topic (event handling notes )1.ppt
1. 1
CSE 331
More Events
(Mouse, Keyboard, Window,
Focus, Change, Document ...)
slides created by Marty Stepp
based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
http://www.cs.washington.edu/331/
2. 2
Mouse events
• Usage of mouse events:
listen to clicks / movement of mouse in a component
respond to mouse activity with appropriate actions
create interactive programs that are driven by mouse activity
• Usage of keyboard events:
listen and respond to keyboard activity within a GUI component
control onscreen drawn characters and simulate text input
• Key and mouse events are called low-level
events because they are close to the
hardware interactions the user performs.
3. 3
MouseListener interface
public interface MouseListener {
public void mouseClicked(MouseEvent event);
public void mouseEntered(MouseEvent event);
public void mouseExited(MouseEvent event);
public void mousePressed(MouseEvent event);
public void mouseReleased(MouseEvent event);
}
• Many AWT/Swing components have this method:
public void addMouseListener(MouseListener ml)
• Mouse listeners are often added to custom components and
drawing canvases to respond to clicks and other mouse actions.
4. 4
Implementing listener
public class MyMouseListener implements MouseListener {
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mousePressed(MouseEvent event) {
System.out.println("You pressed the button!");
}
public void mouseReleased(MouseEvent event) {}
}
// elsewhere,
myComponent.addMouseListener(new MyMouseListener());
Problem: Tedious to implement entire interface when only partial
behavior is wanted / needed.
6. 6
Adapter pattern
• Problem: We have an object that contains the functionality we
need, but not in the way we want to use it.
Cumbersome / unpleasant to use. Prone to bugs.
• Example:
We want to write one or two mouse input methods.
Java makes us implement an interface full of methods we don't want.
• Solution:
Provide an adapter class that connects into the setup we must talk to
(GUI components) but exposes to us the interface we prefer (only have
to write one or two methods).
7. 7
Event adapters
• event adapter: A class with empty implementations of all of a given
listener interface's methods.
examples: MouseAdapter, KeyAdapter, FocusAdapter
Extend MouseAdapter; override methods you want to implement.
• Don't have to type in empty methods for the ones you don't want!
an example of the Adapter design pattern
Why is there no ActionAdapter for ActionListener?
8. 8
An abstract event adapter
// This class exists in package java.awt.event.
// An empty implementation of all MouseListener methods.
public abstract class MouseAdapter implements MouseListener {
public void mousePressed(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
Now classes can extend MouseAdapter rather than
implementing MouseListener.
• client gets the complete mouse listener interface it wants
• implementer gets to write just the few mouse methods they want
• Why did Sun include MouseListener? Why not just the adapter?
9. 9
Abstract classes
• abstract class: A hybrid between an interface and a class.
Defines a superclass type that can contain method declarations (like an
interface) and/or method bodies (like a class).
Like interfaces, abstract classes that cannot be instantiated
(cannot use new to create any objects of their type).
• What goes in an abstract class?
Implementation of common state and behavior that will be inherited
by subclasses (parent class role)
Declare generic behavior for subclasses to implement (interface role)
10. 10
Abstract class syntax
// declaring an abstract class
public abstract class name {
...
// declaring an abstract method
// (any subclass must implement it)
public abstract type name(parameters);
}
• A class can be abstract even if it has no abstract methods
• You can create variables (but not objects) of the abstract type
11. 11
Writing an adapter
public class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent event) {
System.out.println("You pressed the button!");
}
}
// elsewhere,
myComponent.addMouseListener(new MyMouseAdapter());
12. 12
Abstract class vs. interface
• Why do both interfaces and abstract classes exist in Java?
An abstract class can do everything an interface can do and more.
So why would someone ever use an interface?
• Answer: Java has only single inheritance.
can extend only one superclass
can implement many interfaces
Having interfaces allows a class to be part of a hierarchy
(polymorphism) without using up its inheritance relationship.
13. 13
MouseEvent properties
• MouseEvent
public static int BUTTON1_MASK,
BUTTON2_MASK, BUTTON3_MASK,
CTRL_MASK, ALT_MASK, SHIFT_MASK
public int getClickCount()
public Point getPoint()
public int getX(), getY()
public Object getSource()
public int getModifiers() // use *_MASK with this
• SwingUtilities
static void isLeftMouseButton(MouseEvent event)
static void isRightMouseButton(MouseEvent event)
14. 14
Using MouseEvent
public class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent event) {
Object source = event.getSource();
if (source == button && event.getX() < 10) {
JOptionPane.showMessageDialog(null,
"You clicked the left edge!");
}
}
}
15. 15
Mouse motion
public interface MouseMotionListener {
public void mouseDragged(MouseEvent event);
public void mouseMoved(MouseEvent event);
}
• For some reason Java separates mouse input into many interfaces.
The second focuses on the movement of the mouse cursor.
• Many AWT/Swing components have this method:
public void addMouseMotionListener(
MouseMotionListener ml)
• The abstract MouseMotionAdapter class provides empty
implementations of both methods if you just want to override one.
16. 16
Mouse wheel scrolling
public interface MouseWheelListener {
public void mouseWheelMoved(MouseWheelEvent event);
}
The third mouse event interface focuses on the rolling of the mouse's
scroll wheel button.
• Many AWT/Swing components have this method:
public void addMouseWheelListener(
MouseWheelListener ml)
17. 17
Mouse input listener
// import javax.swing.event.*;
public interface MouseInputListener
extends MouseListener, MouseMotionListener,
MouseWheelListener {}
• The MouseInputListener interface combines all kinds of
mouse input into a single interface that you can implement.
• The MouseInputAdapter class includes empty implementations
for all methods from all mouse input interfaces, allowing the same
listener object to listen to mouse clicks, movement, and/or wheel
events.
18. 18
Mouse input example
public class MyMouseInputAdapter extends MouseInputAdapter {
public void mousePressed(MouseEvent event) {
System.out.println("Mouse was pressed");
}
public void mouseDragged(MouseEvent event) {
Point p = event.getPoint();
System.out.println("Mouse is at " + p);
}
}
...
// using the listener
MyMouseInputAdapter adapter = new MyMouseInputAdapter();
myPanel.addMouseListener(adapter);
myPanel.addMouseMotionListener(adapter);
20. 20
KeyListener interface
public interface KeyListener {
public void keyPressed(KeyEvent event);
public void keyReleased(KeyEvent event);
public void keyTyped(KeyEvent event);
}
• Many AWT/Swing components have this method:
public void addKeyListener(KeyListener kl)
• The abstract class KeyAdapter implements all KeyListener
methods with empty bodies.
21. 21
KeyEvent objects
// what key code was pressed?
public static int VK_A, VK_B, ..., VK_Z,
VK_0, ... VK_9,
VK_F1, ... VK_F10,
VK_UP, VK_LEFT, ...,
VK_TAB, VK_SPACE, VK_ENTER, ...
(one for almost every key)
// Were any modifier keys held down?
public static int CTRL_MASK, ALT_MASK, SHIFT_MASK
public char getKeyChar()
public int getKeyCode() // use VK_* with this
public Object getSource()
public int getModifiers() // use *_MASK with this
22. 22
Key event example
public class PacManKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent event) {
char keyChar = event.getKeyChar();
int keyCode = event.getKeyCode();
if (keyCode == KeyEvent.VK_RIGHT) {
pacman.setX(pacman.getX() + 1);
} else if (keyChar == 'Q') {
quit();
}
}
}
// elsewhere,
myJFrame.addKeyListener(new PacKeyListener());
24. 24
Focus events
public interface FocusListener {
public void focusGained(FocusEvent event);
public void focusLost(FocusEvent event);
}
• focus: The current target of keyboard input.
A focus event occurs when the keyboard cursor enters or exits a
component, signifying the start/end of typing on that control.
• Many AWT/Swing components have this method:
public void addFocusListener(FocusListener kl)
• The abstract class FocusAdapter implements all
FocusListener methods with empty bodies.
25. 25
Window events
public interface WindowListener {
public void windowActivated(WindowEvent event);
public void windowClosed(WindowEvent event);
public void windowClosing(WindowEvent event);
public void windowDeactivated(WindowEvent event);
public void windowDeiconified(WindowEvent event);
public void windowIconified(WindowEvent event);
public void windowOpened(WindowEvent event);
}
• A JFrame object has this method:
public void addWindowListener(WindowListener wl)
• The abstract class WindowAdapter implements all
WindowListener methods with empty bodies.
26. 26
Change events
public interface ChangeListener {
public void stateChanged(ChangeEvent event);
}
• These events occur when some kind of state of a given component
changes. Not used by all components, but essential for some.
• JSpinner, JSlider, JTabbedPane, JColorChooser,
JViewPort, and other components have this method:
public void addChangeListener(ChangeListener cl)
27. 27
Component events
public interface ComponentListener {
public void componentHidden(ComponentEvent event);
public void componentMoved(ComponentEvent event);
public void componentResized(ComponentEvent event);
public void componentShown(ComponentEvent event);
}
• These events occur when a layout manager reshapes a component
or when it is set to be visible or invisible.
• Many AWT/Swing components have this method:
public void addComponentListener(ComponentListener cl)
• The abstract class ComponentAdapter implements all
ComponentListener methods with empty bodies.
28. 28
JList/JTree select events
public interface ListSelectionListener {
public void valueChanged(ListSelectionEvent event);
}
public interface TreeSelectionListener {
public void valueChanged(TreeSelectionEvent event);
}
• These events occur when a user changes the
element(s) selected within a JList or JTree.
• The JList component has this method:
public void addListSelectionListener(
ListSelectionListener lsl)
• The JTree component has this method:
public void addTreeSelectionListener(
TreeSelectionListener tsl)
29. 29
Document events
public interface DocumentListener {
public void changedUpdate(DocumentEvent event);
public void insertUpdate(DocumentEvent event);
public void removeUpdate(DocumentEvent event);
}
• These events occur when the contents of a text component (e.g.
JTextField or JTextArea) change.
• Such components have this method:
public Document getDocument()
• And a Document object has this method:
public void addDocumentListener(DocumentListener cl)
• And yes, there is a DocumentAdapter .