{"id":69,"date":"2020-12-31T22:04:23","date_gmt":"2020-12-31T22:04:23","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/practical-java-programming\/?page_id=69"},"modified":"2021-01-11T17:57:25","modified_gmt":"2021-01-11T17:57:25","slug":"events","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/","title":{"rendered":"Events"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2d0c8da11f9\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2d0c8da11f9\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#What_are_Events\" >What are Events?<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Why_are_Events_Important\" >Why are Events Important?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#What_are_Event-Driven_Systems\" >What are Event-Driven Systems?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#The_Event_Queue\" >The Event Queue<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#The_Java_Event_Model\" >The Java Event Model<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#History\" >History<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Why_a_New_Event_Model\" >Why a New Event Model?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Relationship_Between_Models\" >Relationship Between Models<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#The_Java_11_Event_Model\" >The Java 1.1 Event Model<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#When_an_Event_Occurs\" >When an Event Occurs<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#AWT_Event_Hierarchy\" >AWT Event Hierarchy<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Class_Hierarchy\" >Class Hierarchy<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Interface_Hierarchy\" >Interface Hierarchy<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#The_Post-Java_11_Event_Classes_Interfaces\" >The Post-Java 1.1 Event Classes &amp; Interfaces<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/#Swing_Classes_Events\" >Swing Classes &amp; Events<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Before we delve any further into Swing GUI programming, we need to learn about events.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_Events\"><\/span>What are Events?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every GUI platform, be it Microsoft Windows, Motif on Unix, or the Macintosh GUI, employs the concept of events.&nbsp; Different GUI platforms may use different terminology (for example, Microsoft Windows uses the term &#8220;Windows messages&#8221;), but they all use essentially the same concept.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em><strong>Events<\/strong><\/em>&nbsp;can be things like:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Mouse clicks and movement<\/li><li>Keyboard key presses<\/li><li>Window [de]activation, [de]iconification, etc.<\/li><li>System-specific events<\/li><li>Any number of user-defined event types<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Events are delivered using&nbsp;<em><strong>messages<\/strong><\/em>.&nbsp;&nbsp; The operating system usually has a message system which can send and receive messages containing event information.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you (or the system) sends a message, you send it to an object on the system &#8212; a window, a control (button, edit box, list control, whatever), etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Microsoft Windows, Mac, UNIX systems, etc. all use event-driven message sending mechanisms heavily in their windowing support.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note, however, that while events are very important in GUI programming, there are also many other applications where events are useful.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_are_Events_Important\"><\/span>Why are Events Important?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Event and message delivery are a major part of how windowing systems work, and therefore the messaging system must be&nbsp;<em>efficient<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Objects in a windowing system typically sit around waiting to be told what to do &#8212; if they are told to do nothing, they&#8217;ll do exactly that!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Therefore, you need to understand how the events and messaging system work to make things work properly, and to make things work efficiently.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_are_Event-Driven_Systems\"><\/span>What are Event-Driven Systems?<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Anything that typically sits around waiting to be told what to do, and responding to external stimuli, is event-driven.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of an environment which makes the event-driven nature of the system very obvious is&nbsp;<em>Microsoft Visual Basic<\/em>. Lots of programs have been modeled after VB, including many of the Java IDEs.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Event_Queue\"><\/span>The Event Queue<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Typically, each GUI implementation has a single first-in, first-out queue into which each generated event is placed.&nbsp; Events are handled serially, to avoid timing conflicts among multiple events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the Java GUI, there is a special thread, called the Event Queue [Dispatch] Thread.&nbsp; Events are queued to this thread, which processes them in the order in which they were added to the queue.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Java_Event_Model\"><\/span>The Java Event Model<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s a quick summary and overview of the Java event model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Soon, we&#8217;ll show lots of specific examples, so you can see how the model is used.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"History\"><\/span>History<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Originally, in <em>JDK 1.0<\/em>, Java shipped with a simple AWT for portable GUI programming.&nbsp; It used a model for&nbsp;events based on class inheritance:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In order for a program to catch and process GUI events, it must subclass GUI components and override either <strong>action()<\/strong> or <strong>handleEvent()<\/strong> methods.<\/li><li>Returning&nbsp;<strong>true&nbsp;<\/strong>from one of these methods consumes the event so it is not processed further.<\/li><li>Otherwise the event is propagated sequentially up the GUI hierarchy until:<ul><li>it is consumed, or<\/li><li>the root of the hierarchy is reached.<\/li><\/ul><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This means that programs have two choices for structuring their event handling code:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Each individual component can be subclassed to specifically handle its target events; the result is a plethora of classes.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">or:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>All events for an entire hierarchy (or subset) can be handled by a particular container; the result is that the container&#8217;s overridden&nbsp;<strong>action()<\/strong>&nbsp;or&nbsp;<strong>handleEvent()<\/strong>&nbsp;method must contain a complex conditional statement in order to process the events.<\/li><\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong><em><u>Note:<\/u><\/em><\/strong>&nbsp;If you ever find yourself writing <code><strong>action()<\/strong><\/code> or <code><strong>handleEvent()<\/strong><\/code> methods in your Java GUI programs,&nbsp;<strong><em>STOP!<\/em><\/strong>&nbsp; You&#8217;re using the old (prehistoric!) event model.&nbsp; Instead, learn and use the newer, much superior, event model.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Why_a_New_Event_Model\"><\/span>Why a New Event Model?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Java 1.0 event model was simple and well suited for writing basic applets and applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it did not scale well for larger Java programs because:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The requirement to subclass a component in order to make use of its functionality is cumbersome to developers, and improper use of subclassing.<\/li><li>The inheritance model does not lend itself well to maintaining a clean separation between the application model and the GUI, because application code must be integrated directly into the subclassed components at some level.<\/li><li>Since all event types are filtered through the same methods, the logic to process the different event types is complex and error-prone. This becomes worse as new event types are added to the AWT.<\/li><li>There is no filtering of events &#8212; they are always delivered to components whether those components actually handle them or not. This produces a general performance problem, particularly with high-frequency type events such as mouse moves.<\/li><li>For many components, the <strong>action()<\/strong> method passes a <strong>String<\/strong> parameter which is equivalent to either the label of the component (<strong>Button<\/strong>, <strong>MenuItem<\/strong>), or the item selected (<strong>List<\/strong>, <strong>Choice<\/strong>). This often results in poor coding and unwieldy string comparison logic that doesn&#8217;t localize (internationalize) well.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Relationship_Between_Models\"><\/span>Relationship Between Models<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Java 1.0 event model is present, but&nbsp;<em><strong>deprecated<\/strong><\/em>&nbsp;in Java 1.1 and beyond.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The two models can coexist.&nbsp; However, it is very strongly emphasized that you should use only the JDK 1.1+ event model.In extremely rare cases, you may need to use the 1.0 event model for applets that you want to run in old Web browsers that were based on JDK 1.0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, now, all current browsers support recent versions of the JDK (at least JDK 1.3 or later), via the Java Plug-in.&nbsp; The Java Plug-in comes with all recent versions of the JDK, and is installed when you install the JDK.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Note that applets have become pass\u00e9 in recent years.<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Java_11_Event_Model\"><\/span>The Java 1.1 Event Model<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Java 1.1 event model is a&nbsp;<em><strong>Delegation Event Model<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is used by both the GUI and by Java Beans (and virtually everything else that is coming out in the Java world!)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Events are no longer represented by a single event class (like <code><strong>java.awt.Event<\/strong><\/code>) with numeric ids.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead, events are represented by a hiearchy of event classes:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code><strong>ActionEvent<\/strong>,&nbsp;<strong>WindowEvent<\/strong>,&nbsp;<strong>MouseEvent<\/strong>,&nbsp;<strong>KeyEvent<\/strong><\/code>, etc.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Since a single event class may be used to represent more than one event type (e.g.&nbsp;<code><strong>MouseEvent<\/strong><\/code>&nbsp;represents mouse up, mouse down, mouse drag, mouse move, etc.), some event classes may also contain an&nbsp;<strong>id<\/strong>&nbsp;(unique within that class) which maps it to its specific event types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Every event is a subclass of&nbsp;<strong><code>java.util.EventObject<\/code><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">GUI Events are subclasses of&nbsp;<code><strong>java.awt.AWTEvent<\/strong><\/code>&nbsp;(which itself is a subclass of&nbsp;<code><strong>EventObject<\/strong><\/code>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If a class is interested in an event, it declares itself to be a&nbsp;<em>listener<\/em>&nbsp;for that particular class of event.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_an_Event_Occurs\"><\/span>When an Event Occurs<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>When a object that generates an event needs to tell a <em>listener<\/em> object that an event happened, the system:<ul><li>Calls the appropriate method of the <em>listener interface<\/em><\/li><li>Passes to that method an object of the appropriate type the extends from&nbsp;<code><strong>EventObject<\/strong><\/code><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"AWT_Event_Hierarchy\"><\/span>AWT Event Hierarchy<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The AWT Event classes and related interfaces form an inheritance hierarchy. Below is the inheritance hiearchy for Java 1.5.0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that there are a few event classes and their associated listener interfaces that we will not be talking about;&nbsp; we will concentrate on the most commonly used events.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Class_Hierarchy\"><\/span>Class Hierarchy<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>java.lang.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/lang\/Object.html\"><strong>Object<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ComponentAdapter.html\"><strong>ComponentAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ComponentListener.html\">ComponentListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ContainerAdapter.html\"><strong>ContainerAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ContainerListener.html\">ContainerListener<\/a>)<\/li><li>java.util.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/util\/EventListenerProxy.html\"><strong>EventListenerProxy<\/strong><\/a>&nbsp;(implements java.util.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/util\/EventListener.html\">EventListener<\/a>)<ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/AWTEventListenerProxy.html\"><strong>AWTEventListenerProxy<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/AWTEventListener.html\">AWTEventListener<\/a>)<\/li><\/ul><\/li><li>java.util.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/util\/EventObject.html\"><strong>EventObject<\/strong><\/a>&nbsp;(implements java.io.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/io\/Serializable.html\">Serializable<\/a>)<ul><li>java.awt.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/AWTEvent.html\"><strong>AWTEvent<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ActionEvent.html\"><strong>ActionEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/AdjustmentEvent.html\"><strong>AdjustmentEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ComponentEvent.html\"><strong>ComponentEvent<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ContainerEvent.html\"><strong>ContainerEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/FocusEvent.html\"><strong>FocusEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/InputEvent.html\"><strong>InputEvent<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/KeyEvent.html\"><strong>KeyEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseEvent.html\"><strong>MouseEvent<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseWheelEvent.html\"><strong>MouseWheelEvent<\/strong><\/a><\/li><\/ul><\/li><\/ul><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/PaintEvent.html\"><strong>PaintEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowEvent.html\"><strong>WindowEvent<\/strong><\/a><\/li><\/ul><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/HierarchyEvent.html\"><strong>HierarchyEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/InputMethodEvent.html\"><strong>InputMethodEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/InvocationEvent.html\"><strong>InvocationEvent<\/strong><\/a>&nbsp;(implements java.awt.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/ActiveEvent.html\">ActiveEvent<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ItemEvent.html\"><strong>ItemEvent<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/TextEvent.html\"><strong>TextEvent<\/strong><\/a><\/li><\/ul><\/li><\/ul><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/FocusAdapter.html\"><strong>FocusAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/FocusListener.html\">FocusListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/HierarchyBoundsAdapter.html\"><strong>HierarchyBoundsAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/HierarchyBoundsListener.html\">HierarchyBoundsListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/KeyAdapter.html\"><strong>KeyAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/KeyListener.html\">KeyListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseAdapter.html\"><strong>MouseAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseListener.html\">MouseListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseMotionAdapter.html\"><strong>MouseMotionAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseMotionListener.html\">MouseMotionListener<\/a>)<\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowAdapter.html\"><strong>WindowAdapter<\/strong><\/a>&nbsp;(implements java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowFocusListener.html\">WindowFocusListener<\/a>, java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowListener.html\">WindowListener<\/a>, java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowStateListener.html\">WindowStateListener<\/a>)<\/li><\/ul><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Interface_Hierarchy\"><\/span>Interface Hierarchy<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<ul class=\"wp-block-list\"><li>java.util.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/util\/EventListener.html\"><strong>EventListener<\/strong><\/a><ul><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ActionListener.html\"><strong>ActionListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/AdjustmentListener.html\"><strong>AdjustmentListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/AWTEventListener.html\"><strong>AWTEventListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ComponentListener.html\"><strong>ComponentListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ContainerListener.html\"><strong>ContainerListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/FocusListener.html\"><strong>FocusListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/HierarchyBoundsListener.html\"><strong>HierarchyBoundsListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/HierarchyListener.html\"><strong>HierarchyListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/InputMethodListener.html\"><strong>InputMethodListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/ItemListener.html\"><strong>ItemListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/KeyListener.html\"><strong>KeyListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseListener.html\"><strong>MouseListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseMotionListener.html\"><strong>MouseMotionListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/MouseWheelListener.html\"><strong>MouseWheelListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/TextListener.html\"><strong>TextListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowFocusListener.html\"><strong>WindowFocusListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowListener.html\"><strong>WindowListener<\/strong><\/a><\/li><li>java.awt.event.<a href=\"http:\/\/java.sun.com\/j2se\/1.5.0\/docs\/api\/java\/awt\/event\/WindowStateListener.html\"><strong>WindowStateListener<\/strong><\/a><\/li><\/ul><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Post-Java_11_Event_Classes_Interfaces\"><\/span>The Post-Java 1.1 Event Classes &amp; Interfaces<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The events available in the Java 1.1+ model may be classified into a set of event classes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Associated with each event class is a corresponding&nbsp;<strong><em>listener interface<\/em><\/strong>, containing a&nbsp;<strong><em>set of listener methods<\/em><\/strong>, and, for those listeners which have more than a single method, an&nbsp;<strong><em>associated adapter class<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Event Class<\/th><th>Type<\/th><th>Listener Interface<\/th><th>Listener Methods<\/th><th>Adapter Class<\/th><\/tr><tr><td><code><strong>ActionEvent<\/strong><\/code><\/td><td>Semantic<\/td><td><code><strong>ActionListener<\/strong><\/code><\/td><td><code>actionPerformed()<\/code><\/td><td><code>&nbsp;<\/code><\/td><\/tr><tr><td><code><strong>AdjustmentEvent<\/strong><\/code><\/td><td>Semantic<\/td><td><code><strong>AdjustmentListener<\/strong><\/code><\/td><td><code>adjustmentValueChanged()<\/code><\/td><td><code>&nbsp;<\/code><\/td><\/tr><tr><td><code><strong>ComponentEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>ComponentListener<\/strong><\/code><\/td><td><code>componentHidden()<br>componentMoved()<br>componentResized()<br>componentShown()<\/code><\/td><td><code>ComponentAdapter<\/code><\/td><\/tr><tr><td><code><strong>ContainerEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>ContainerListener<\/strong><\/code><\/td><td><code>componentAdded()<br>componentRemoved()<\/code><\/td><td><code>ContainerAdapter<\/code><\/td><\/tr><tr><td><code><strong>FocusEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>FocusListener<\/strong><\/code><\/td><td><code>focusGained()<br>focusLost()<\/code><\/td><td><code>FocusAdapter<\/code><\/td><\/tr><tr><td><code><strong>ItemEvent<\/strong><\/code><\/td><td>Semantic<\/td><td><code><strong>ItemListener<\/strong><\/code><\/td><td><code>itemStateChanged()<\/code><\/td><td><code>&nbsp;<\/code><\/td><\/tr><tr><td><code><strong>KeyEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>KeyListener<\/strong><\/code><\/td><td><code>keyPressed()<br>keyReleased()<br>keyTyped()<\/code><\/td><td><code>KeyAdapter<\/code><\/td><\/tr><tr><td><code><strong>MouseEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>MouseListener<\/strong><\/code><\/td><td><code>mouseClicked()<br>mouseEntered()<br>mouseExited()<br>mousePressed()<br>mouseReleased()<\/code><\/td><td><code>MouseAdapter<\/code><\/td><\/tr><tr><td> <\/td><td> Low-level<\/td><td><code><strong>MouseMotionListener<\/strong><\/code><\/td><td><code><strong>mouseDragged()<br>mouseMoved()<\/strong><\/code><\/td><td><code><strong>MouseMotionAdapter<\/strong><\/code><\/td><\/tr><tr><td><code><strong>MouseWheelEvent<\/strong><\/code><\/td><td>Low-Level<\/td><td><code><strong>MouseWheelListener<\/strong><\/code><\/td><td><code>mouseWheelMoved()<\/code><\/td><td>&nbsp;<\/td><\/tr><tr><td><code><strong>TextEvent<\/strong><\/code><\/td><td>Semantic<\/td><td><code><strong>TextListener<\/strong><\/code><\/td><td><code>textValueChanged()<\/code><\/td><td><code>&nbsp;<\/code><\/td><\/tr><tr><td><code><strong>WindowEvent<\/strong><\/code><\/td><td>Low-level<\/td><td><code><strong>WindowListener<\/strong><\/code><\/td><td><code>windowActivated()<br>windowClosed()<br>windowClosing()<br>windowDeactivated()<br>windowDeiconified()<br>windowIconified()<br>windowOpened()<\/code><\/td><td><code>WindowAdapter<\/code><\/td><\/tr><tr><td> <\/td><td> <\/td><td><code><strong>WindowFocusListener<\/strong><\/code><\/td><td><code><strong>windowGainedFocus()<br>windowLostFocus()<\/strong><\/code><\/td><td> <\/td><\/tr><tr><td> <\/td><td> <\/td><td><code><strong>WindowStateListener<\/strong><\/code><\/td><td><code><strong>windowStateChanged()<\/strong><\/code><\/td><td> <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Note that all&nbsp;<em>low-level events<\/em>&nbsp;extend from <strong>ComponentEvent<\/strong>.&nbsp; All other events are classified as&nbsp;<em>semantic events.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Swing_Classes_Events\"><\/span>Swing Classes &amp; Events<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><em>JFC(Java Foundation Classes)\/Swing<\/em> contains a very large number of classes, and so coming up with a table of events used by each Swing component is difficult, and would become obsolete very quickly.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that many of the JFC\/Swing classes, such as&nbsp;<code><strong>JFrame<\/strong><\/code>,&nbsp;<code><strong>JPanel<\/strong><\/code>, etc., are subclasses of AWT component and other Swing component classes, and so inherit some of their event generation behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To determine what events are generated by a JFC\/Swing class, first look at the javadoc for that class:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Read the description of the class;&nbsp; often there is a description of (some of) the events that the class generates, and under what circumstances.&nbsp; Note that you may find that it&#8217;s buried somewhat, so read it thoroughly!<\/li><li>Look for methods in the class with the signature:<br><code><strong>public void add<em>Mumble<\/em>Listener(<em>Mumble<\/em>Event e)<\/strong><\/code>  <br>The presence of such a method indicates that the class generates events that can be listened for. ; The <strong><em>Mumble<\/em><\/strong> represents the type of event.<\/li><li>Now look at each superclass, following the same process, until you reach the top of the inheritance hierarchy.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Before we delve any further into Swing GUI programming, we need to learn about events. What are Events? Every GUI platform, be it Microsoft Windows, Motif on Unix, or the Macintosh GUI, employs the concept of events.&nbsp; Different GUI platforms may use different terminology (for example, Microsoft Windows uses the term &#8220;Windows messages&#8221;), but they [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":34,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-69","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Before we delve any further into Swing GUI programming, we need to learn about events. What are Events? Every GUI platform, be it Microsoft Windows, Motif on Unix, or the Macintosh GUI, employs the concept of events.&nbsp; Different GUI platforms may use different terminology (for example, Microsoft Windows uses the term &#8220;Windows messages&#8221;), but they&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":6,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69\/revisions\/756"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/34"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}