Generating Event Listening and Handling Methods


The IDE relieves you of the task of providing the infrastructure required around event handling by generating the code to link the occurrence of the event with the invocation of a private method in the form class. For example, a JButton named myBtn might have the code

myBtn.addActionListener(new java.awt.event.ActionListener() {     public void actionPerformed(java.awt.event.ActionEvent evt) {         myBtnActionPerformed(evt);     }


added to its initialization, where the method myBtnActionPerformed is generated as:

 private void myBtnActionPerformed(java.awt.event.ActionEvent evt) {      // TODO add your handling code here:  }


Commentsgenerated or otherwisewithin your Java code that start with "TODO" have special significance. To see a list of your "TODO" lines, display the To Do window via Window | To Do or press Ctrl-6. From the displayed window, you can navigate to the source line with the TODO with a double-click in the To Do window. See Tracking Notes to Yourself in Your Code in Chapter 5 for more information.


Generation of this event infrastructure code can be done in a few different ways:

  • By right-clicking the component and choosing the event to be handled from the Events menu.

    The IDE generates the event handler and positions the cursor to the appropriate TODO line in the generated private method for completion of the event handling code.

  • By selecting the component and specifying the event to be handled in the Events tab of the Properties window.

  • By using the Connection Wizard to generate code for the case when an event on a component should result in the modification of another component.

To use the Connection wizard:

  1. Enter "connection mode" by clicking the icon in the Form Editor's toolbar.

  2. Open the Connection Wizard by clicking successively on the two componentsfirst the component that will fire the event and then the component upon which an operation is to be performed.

  3. In the Select Source Event page of the wizard (shown in Figure 6-9), select the event to be fired.

    Figure 6-9. Connection Wizard, Select Source Event page

  4. In the Specify Target Operation page (shown in Figure 6-10), specify the operation to be performed on the target component. You can specify a property to set, call a method, or write your own custom code.

Figure 6-10. Connection Wizard, Specify Target Operation page


The Connection Wizard approach is simply a "point and click" approach to the task. The code generated by the wizard is not guarded and can be modified in the editor after generation.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net