Handling Events With User-Defined Methods

Contents


Note on Handling Events in Imported Classes

Before handling events in classes you've imported, see Imported Class Limitations.


Using a New Method

  1. Display the object's resources in the Resources card.

  2. In the Resources card, scroll to the Event Handlers section.

  3. Type the method name in the field next to the event type that you want handled.

    The visual builder generates a placeholder for the method in the .java file.

  4. Choose File > Save from the visual builder menu bar.

  5. In the Source panel of Cosmo Code's main window, open the .java file.

  6. Find the comment that begins with //{{ extra_code

    For example, if your method is getName( ), you would see the following in the .java file:

    //{{ extra_code
    
     public void getName (java.awt.event.ActionEvent event) { 
    
         //{{ event_handler
    
         // Add your code here
    
     } //}} event_handler
    
     //}} extra_code
    

    All placeholders for undefined methods are written between the extra_code comments. Once a method is defined, you are free to move it anywhere in the code.

  7. Define the method between the beginning and ending comments //{{ event_handler.

    IMPORTANT: For the method to be called, Cosmo Code looks for the argument list(Event-Type event) and the comments //{{ event_handler at the beginning and ending of your method. The argument list must not be changed, and the comments must stay with the code definition.

Using a Previously Defined Method

  1. Display the .java file in the Source panel.

  2. Write your method with an argument list of (Event-Type event) and the comment //{{ event_handler at the beginning and ending of the method.

    For example:

    public void removeName (java.awt.event.ActionEvent event) {
       //{{ event_handler
       int index = nameList.getSelectedIndex();
          
       if (index != -1) {
           nameList.delItem (index);
        }
    }                                    //}} event_handler 
    

    IMPORTANT: For the method to be called, Cosmo Code looks for the argument list(Event-Type event) and the comments //{{ event_handler at the beginning and ending of your method. The argument list must not be changed, and the comments must stay with the code definition.

  3. In the visual builder, display the object's resources in the Resources card.

  4. In the Resources card, scroll to the Event Handlers section.

  5. Type the previously defined method name in the field next to the event type that you want handled.

  6. Choose File > Save from the visual builder menu bar.

  7. In the Source panel of Cosmo Code's main window, open the .java file.

Converting a Previously Defined 1.0.2 Method to a 1.1 Method

If you are using a previously defined 1.0.2 method, you'll need to create a 1.1 method using the Object Manager and copy the method body from the 1.0.2 method into the 1.1 method. You may need to do some editing to adjust the syntax.

Note: While you can mix event styles, using only 1.1 style events provides better performance. The only exception to mixing event styles is if you use the same kind of event. For example, if you use a 1.1 event and it's deprecated 1.0.2 equivalent, then only the 1.1 event will ever be triggered.

Guide to the Cosmo Code Development Environment
12-96*527