You can add an event to a component in the Design view or Java Beans view of the visual editor for Java.
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 JavaBean 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 will be re-used if it is added to the JavaBean 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 will extend this. Otherwise, it will implement 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 will be executed when the event occurs, and you should 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 already exists. If this is a listener that extends the adapter, the same listener will be re-used 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 JavaBean as shown previously, but they can also be added to a listener in the Java Beans tree. The Events menu shows all of the event callback methods on the listener, and any that are already used are disabled.
For a PropertyChangeListener the pop-up menu shows all of the bound properties on the JavaBean. If any are already used by the PropertyChangeListener then they are disabled.
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 JavaBean using the two argument method addPropertyChangeListener(String propertyName, PropertyChangeListener listener) then it is specific to a particular property so cannot be re-used for another property. In this case all of the Events cascade menu children are disabled.