Processing Timer Events is part of javax.swing helps manage activity over time Use it to set up a timer to generate an action event periodically When a timer event takes place, timer calls actionPerformed(ActionEvent event) part of the ActionListener interface (java.awt.event) Example: TimerTester.java
Inner classes Inner class Any class defined inside another is available to all the methods of the enclosing class Can access the members of the enclosing class Can reference the outer class using (Outerclass.this)
Anonymous inner classes Anonymous classes Often encountered within a method Combines the creation of an object with the definition Requires the inclusion of a semi-colon marking the end of expression containing the anonymous class Objects defined inside the method but outside of class Must be final before used inside anonymous class
Graphical Applications Java apps having graphical components Called GUI components GUI component An object representing a screen element Some are containers used to hold other components GUI related classes Defined in java.awt and javax.swing packages
GUI containers Frame Panel Used to display GUI-based Java application Displayed as a window with a title bar A heavyweight container Managed by the underlying operating system Panel Container that is not stand-alone Must be added to another container to be displayed Used to organize other components Lightweight container managed by the java program itself
Example: Authority Label Let us look at a program GUI component displaying a line of text Used to display information or to identify other components Let us look at a program Organizing two labels in a panel And displaying panel in a frame
GUI interfaces GUI revolves around Event Listener Components Events Listeners Event Represents some activity to which we may want to respond May be generated by a graphical component Listener Defines what happens when an event occurs
A corresponding listener Events and Listeners Event Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event © 2004 Pearson Addison-Wesley. All rights reserved
Example: PushCounter A push button The pushcounter example Allows the user to initiate an action By pressing a graphical button using the mouse is defined by JButton class The pushcounter example Displays a push button Increments a counter each time a button is pushed
Example: TextFields A text field Fahrenheit.java Allows the user to enter one line of input Generates an event when the enter key is pressed Fahrenheit.java Instantiates a listener and adds it to text field When user enters temperature in Fahrenheit The conversion into Celsius is performed