Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

I. GUI Components and Event Handling > I.5. How Event Handling Works

I.5. How Event Handling Works

Let’s illustrate how the event-handling mechanism works, using textField1 from the example of Fig. I.1. We have two remaining open questions from Section I.3:

1. How did the event handler get registered?

2. How does the GUI component know to call actionPerformed rather than some other event-handling method?

The first question is answered by the event registration performed in lines 43–46 of Fig. I.1. Figure I.4 diagrams JTextField variable textField1, TextFieldHandler variable handler and the objects to which they refer.

Image

Fig. I.4. Event registration for JTextField textField1.

Registering Events

Every JComponent has an instance variable called listenerList that refers to an object of class EventListenerList (package javax.swing.event). Each object of a JComponent subclass maintains references to its registered listeners in the listenerList. For simplicity, we’ve diagramed listenerList as an array below the JTextField object in Fig. I.4.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint