Adding Event Listeners

The next step after designing and building a Desktop application is to add behaviors to it.

In Swing, this is done by implementing Event Listeners. Swing Components register specific objects, the Event Listeners, in order to perform a determined task. When an event is triggered, the Listeners capture this call and handle the action according to what was implemented. Listeners also provide Events to those methods.

There are multiple kinds of events in Swing that range from mouse, focus, key, and window events.

For a more detailed view of Event Listeners, visit:

http://download.oracle.com/javase/tutorial/uiswing/events/intro.html

Getting ready

It is necessary to have the sources of a Java Desktop Application recipe in your NetBeans project folder. Many of the files created in this recipe build upon previous recipes in this chapter, because we wish to give continuity to the recipes.

Tip

If you are unsure how to do these steps, please refer to the previous recipes in this chapter. The project and file name references come from the other recipes in this chapter, so feel free to use the same naming if you're not feeling confident.

The previous recipes Creating a Panel and Creating a Frame would be of great help in understanding why the design and components listed here are presented in this manner.

How to do it...

Open MyBaseJPanel.java in the GUI Editor with the Design mode selected.

To add an Event Listener:

  1. Right-click on the Clear... button and select events, mouse, and then click on mouseClicked.
  2. The Java editor will open up in Source mode. Inside the method, replace the TODO comment in jButton1MouseClicked with the following code:
    jTextField1.setText("");
    jTextField2.setText("");
    jTextField3.setText("");
    
  3. Save the file.
  4. Then add MyBaseJPanel.java to MyJFrame.java, if MyJFrame already contains MyBaseJPanel, then remove it, and press Shift+F6(shortcut).
How to do it...

Now it is possible to write and clear the text that was placed inside the TextFields.

How it works...

When the sub-menu entry, the mouseClicked event is selected, NetBeans adds a mouse listener implementation to the jButton1 object.

This can be seen by expanding the IDE-generated code block in the IDE. It is not possible to edit this code directly, since it is auto-generated by NetBeans. Changes to the implementation must be done from within the Properties window.

This is the relevant part of the IDE-generated code:

jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});

The MouseAdapter is the interface used by the triggered event when a mouse is clicked. There are multiple methods in the MouseAdapter but we chose mouseClicked for this example.

NetBeans then adds a method, the jButton1MouseClicked method, with an empty implementation so the developer can add a behavior to it. In our case, we change the values of the Text Fields to empty Strings, clearing out their content.

There's more...

Unsure how to edit the code generated by the IDE?

Components Properties Window

Every component has properties and all of those properties can be edited directly using the NetBeans Properties window.

Placed on the left-hand side of the editor, this window will become available whenever a component is selected and will list the available options for that component.

Some of the options available in the Properties window are:

  • Properties: Related to properties of the component, such as name, color, position, alignment, icon, and other configurations.
  • Binding: Binds Components; for example, if you wish to update the progress of a status bar on a label, it is possible to do this by binding the components together. It's also possible to bind components with information held in a database or JavaBeans.
  • Events: An alternative way of adding events instead of right-clicking on the component. Click on the drop-down menu and NetBeans will suggest a name for a method. If the suggested name is selected, NetBeans will change the view into Design mode, place the cursor inside the selected method, and enter the developer code.
  • Code: Here it is possible to insert code inside the NetBeans-generated block. This is achieved by using the Pre and Post combination of various factors, such as Pre-init, Post-Listener, and Pre-adding. We can also configure other variable-related properties like visibility and name of the variable.
Components Properties Window

It is also possible to access the properties window by right-clicking on the component one wishes to know about and selecting properties. A new pop-up will be launched by NetBeans with the same contents as in the screenshot above.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.76.200