Adding events to a component

You can add an event to a component in the Design view or Java™ Beans view of the visual editor for Java.

About this task

To add an event to a component:

Procedure

  1. Right-click the component in the Java Beans view or the Design view.
  2. From the pop-up menu, click Events. The pop-up menu shows the preferred events for the component. Do one of the following steps:
    • Click one of the preferred events for the component. The event is added to the component. For example, for a JFrame the preferred events are windowClosed and windowOpened.
    • Click Add Events to open the Add Events window.
  3. If you opened the Add Events window, you are presented with a list of categorized events to choose from. Select the event that you want to add, then click Finish.
    • The Add Event dialog shows all the available events for the component in a tree. The tree shows the event listeners as the first level of entries, and beneath each listener entry are the possible callbacks the event can raise.
      add event dialog
    • You can use the dialog to specify whether you want to create a listener or use an existing one. When the dialog opens, it analyzes the existing listeners, and if it finds one that it believes can be reused, it marks the listener with a different icon. For existing listeners that extend an adapter class, the event present class icon is used, otherwise, the event present is used. Within the listener itself, existing events are shown with a green arrow green arrow, and available events are shown with an incomplete arrow unevent
    • You can specify whether you want to use the existing listener or create a new one. When a new one is created, you can choose whether you want it to extend the adapter class or implement the listener interface. If an option is not applicable, the radio buttons are disabled. For example, if there is no existing listener, or an existing listener is selected but the selected event is already used, the Use existing Listener is disabled. If there is no available adapter class for a new listener then the extends: ADAPTER_CLASS_NAME radio button is disabled.
    • If you select propertyChange in the Add Event dialog, you have additional options for adding the callback. A new listener can be created that can be added to the Java bBean using the method a addPropertyChange(PropertyChangeListener listener). If this occurs then the listener's generic callback method propertyChange(PropertyChangeEvent event) is used for all property events.  On some JavaBeans (such as Swing components) the two argument method addPropertyChange(String propertyName, PropertyChangeListener listener) is available and selected by default to use for the new listener.

      add event dialog

      A listener that is added using the two argument method is specific to a particular property, so it cannot be reused for another property. A listener added with a single argument method has an if statement that checks the name of the property before processing the logic for each property callback as shown in the following code:

      javaBean.addPropertyChangeListener(new java.beans.PropertyChangeListener() { 
           public void propertyChange(java.beans.PropertyChangeEvent e) {  
              if ((e.getPropertyName().equals("font"))) {  
                  System.out.println("propertyChange(font)");   
             }  
          }  
      });

      This allows a PropertyChangeListener added with a single argument method to be used for more than one property (by having multiple if{} blocks used).


Results

When you add an event, if there is an existing listener that can be used, then the callback method is added to it. Otherwise, a new listener is created. For an existing listener to be used, it must be an anonymous inner class on the Java bean that implements the listener interface. It must have an empty method body for the callback method, or extend the adapter class. and have no existing method for the event callback being added. For a property, an existing PropertyChangeListener is reused if it is added to the Java bean with the single argument method addPropertyChange(PropertyChangeListener listener). If it does not already have code, processing the property is added.

If there is no candidate existing listener onto which the callback method can be added, then a new listener is created. This will be an anonymous inner class, and if an adapter class has been defined for the event then the listener extends this. Otherwise, it implements the listener interface. After the event is added a stub method is created with a //TODO comment. The stub method is an indicator of the source code that is executed when the event occurs, and you can then change this to perform your required behavior. The //TODO comment is displayed in the Tasks window, flagging incomplete methods. This is so that you can locate them later and remove the //TODO comment once the callback logic has been written.

In the previous example the windowOpened method exists. If this is a listener that extends the adapter, the same listener is reused because it does not already have a windowClosed method. The method windowClosed(WindowEvent e) is added, and the method stub and //TODO comment added as shown here:

this.addWindowListener(new java.awt.event.WindowAdapter() {       
        public void windowClosed(java.awt.event.WindowEvent e) {  
         System.out.println("windowClosed()"); // TODO Auto-generated stub windowClosed()       
              }       
        public void windowOpened(java.awt.event.WindowEvent e) {          
             callExistingWindowOpenedLogic();      
            }  
});

In expert mode, events can still be added to the Java bean as shown previously, but they can also be added to a listener in the Java Beans tree. The Events menu shows all the event callback methods on the listener, and any that are already used are disabled.

add window closed

For a PropertyChangeListener, the pop-up menu shows all the bound properties on the Java bean.  If any are already used by the PropertyChangeListener then they are disabled.

property change listener

A listener added with a single argument method has an if statement that checks the name of the property before processing the logic for each property callback as shown in the following code:

javaBean.addPropertyChangeListener(new java.beans.PropertyChangeListener() {  
    public void propertyChange(java.beans.PropertyChangeEvent e) {                        
             if ((e.getPropertyName().equals("font"))) {              
                   System.out.println("propertyChange(font)");         
             }     
          }  
    });

This allows a PropertyChangeListener added with a single argument method to be used for more than one property (by having multiple if{} blocks used), and when the second and subsequent property callback is added a new if{} blocks are added.

If the propertyChangeListener is added to the Java bean using the two argument method addPropertyChangeListener(String propertyName, PropertyChangeListener listener) then it is specific to a particular property so cannot be reused for another property. In this case all the Events cascade menu children are disabled.


Feedback