Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Writing Event Handlers

Event code is executed upon a given associated action. This is programming based on the event-driven programming model. In other words, code is executed as a reaction to a given event or events. This code is written within an event-handling stub to allow for better organization of code. JBuilder then manages the relationship or the attachment of these code snippets or business logic to the corresponding component's event.

Using the Events tab of the Inspector, you can easily attach a new or modify an existing event handler. To attach event-handling code to a component event, you need to do the following:

  1. Choose the appropriate component by selecting it on the user interface or in the Component tree.

  2. Choose the Event page of the Inspector.

  3. Find the appropriate event you want to add a handler to and double-click on it.

NOTE

After completing the first three steps, you will be positioned in the implementation section of the code to enter the event business logic.

  1. Write your business logic to implement the desired effect.

Just like in the Screen Designer, JBuilder does not add any special tags; it simply inserts standard Java code into your source file. In Listing 3.2, you will see the code generated by JBuilder.

Listing 3.2 The Code Generated to Respond to an actionPerformed Event

  jButton1.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(ActionEvent e) {
    jButton1_actionPerformed(e);
   }
  });
...
 void jButton1_actionPerformed(ActionEvent e) {

 }

How an Event Is Processed

Many things have to be aligned properly for an event to be processed. When you first look at an event handler in Java, it can be overwhelming. Objects have to be created and assigned to the proper handler. The beauty of its design and implementation is that it works and gives you lots of flexibility for future design. To create an event handler for a given action, see Figure 3.6 and follow the five steps:

Figure 3.6Figure 3.6 The interrelationships for event processing. Each number on the diagram represents a snippet of code included in the five steps.

The process for creating an event handler, although it does not contain much code, is actually not as easy as it looks. The process to create these lines of code by hand is defined in the following five steps:

  1. Create an Adapter object. More specifically in this instance, we are creating an Adapter object of the ActionListener type.

      jButton1.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
       }
      });
  2. Register the adapter with the component.

      jButton1.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
       }
      });
  3. Create an Event object.

  4. Send the Event to the Adapter.

      jButton1.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
       }
      });
  5. Send the Event to the original object.

      jButton1.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
       }
      });

Just like in the rest of JBuilder, events can also be added by means of a shortcut. It is as simple as double-clicking on the component in the Screen Designer, and the default action is attached. For example, the default action for a jButton, defined within its BeanInfo class, is actionPerformed. To this point, we have ignored that there is more than one method for implementing an adapter. You have at your disposal two different adapter types: anonymous and standard.

Using Anonymous Adapters

Anonymous adapters use a special convention in Java that is seldom used in other places. That is Java's capability to define an inner class. An inner class is defined within or inside another class. Because an adapter is defined inside the frame and is owned by the frame, it has full access to all the methods and properties of that class. Listing 3.3 shows an example of an anonymous adapter. Contrast that with Listing 3.4, which is a standard adapter implementation.

Listing 3.3 An Example of an Anonymous Adapter Implementation

  jButton1.addActionListener(new java.awt.event.ActionListener() {
   public void actionPerformed(ActionEvent e) {
    jButton1_actionPerformed(e);
   }
  });

Using Standard Adapters

Standard adapters are only standard in that they allow for an adapter to be defined externally from a class before Java allowed inner classes. Using anonymous adapters is actually more of the norm in user interface construction today. Listing 3.4 demonstrates this in an implementation of actionPerformed.

Listing 3.4 An Example of a Standard Adapter Implementation

  jButton1.addActionListener(new FrmDemo_jButton1_actionAdapter(this));
...
class FrmDemo_jButton1_actionAdapter implements 
_ java.awt.event.ActionListener {
 FrmDemo adaptee;

 FrmDemo_jButton1_actionAdapter(FrmDemo adaptee) {
  this.adaptee = adaptee;
 }
 public void actionPerformed(ActionEvent e) {
  adaptee.jButton1_actionPerformed(e);
 }
}

Changing Adapter Settings

Changing the adapter style is easy in most cases, although the anonymous adapter is normally used in JDK 1.1.3 and greater development. To change the object adapter type, you must use the Project Properties dialog box. Figure 3.7 shows the Code Style tab of the Project Properties page.

Figure 3.7Figure 3.7 The Project Properties page/Code Style tab affects event processing code construction by JBuilder.

Following these steps enables you to change your adapter style generated by JBuilder:

  1. Select Project, Project Properties.

  2. Select the Code Style tab.

  3. Make an adapter choice (Standard—External Class, Anonymous—Internal Class).

  4. Decide whether to Match Existing Code—use the same adapter as the others defined within this frame/class.

  • + Share This
  • 🔖 Save To Your Account