| Developer's Daily | JBuilder Education |
| main | java | perl | unix | DevDirectory |
Introduction
When I first began working with JBuilder, I stumbled onto a technique that I now use to help understand user interface events that I'm not familiar with. For example, when developing a TreeControl component, how does the focusGained event work? Or, looking at the same problem from a different angle, how do I know when the user selects a node in the tree component, so I can create code to respond to that event?
In this article, I'll present this simple learning tool, so you can
use it in your code to better understand user-interface events as the occur.
This technique works not only with the TreeControl component, but with
all other user interface components as well.
The method
The basic technique to understanding GUI component events is very simple, and requires just a little setup. Basically, two things are required. First, you need to create a small JBuilder application -- not an applet -- because you'll be writing output to the DOS window that starts the application. Second, the application needs to include the GUI component that you want to learn about.
For the purposes of this article, we'll use the TreeControl application
developed in one of our
previous articles. This application meets both of my setup criteria
- it's a small application, and it includes the TreeControl component that
I want to learn more about.
Setting up the test application
To create the test application, follow the first several steps of the accompanying TreeControl article until you complete the code shown in Listing 1 of that article. Don't worry if you've already gone further than the code shown in that listing. Just make sure your test application includes the TreeControl component.
Once you create the sample application, make sure you're in the Design view of the AppBrowser. Click on the treeControl1 component in the UI Designer Design pane. Then, click on the Inspector window. The title of the Inspector window should now read "treeControl1-Inspector".
Next, click on the Events tab of the Inspector window to display the list of events of the TreeControl component. At this point your Inspector window should look like the Inspector frame shown in Figure 1.
| Figure 1: | The Inspector frame shows these events for the treeControl1 object. |
Testing TreeControl events
Let's assume for a moment that you're curious about the mouseClicked event. Perhaps you don't understand when this event is triggered by the end-user's actions. For instance, you may wonder if this event is triggered every time the user clicks the mouse, or if it's only triggered when the end-user clicks on the nodes of the tree.
To test the mouseClicked event, double-click on the mouseClicked text field (the right-hand column). This creates some text in this field. This is the name of an event-handling method that JBuilder is creating for you. For now, don't worry about that text, and double-click in this field again. When you do this, the Inspector window disappears, and JBuilder switches you to the Source view of the Java source code for the TreeControl frame.
In the source code, JBuilder creates the skeleton of a mouseClicked
method for you. This skeleton is called a stub event-handling
method. This skeleton method should look like this:
}
At this time, if you knew when the mouseClicked event was triggered, you'd place the desired event-handling code in this method. But, assuming for now that we don't know when the mouseClicked event is triggered, we'll take a different approach. To learn more about when this event is triggered, we're simply going to write output to the DOS window each time this method is called.
To learn when this method is called, add the following line of code to the treeControl1_mouseClicked() method:
Next, run the application by clicking the Run icon. When the application starts, move the DOS window to a place on the screen where you can see both this window and the TreeControlFrame at the same time. Then, start experimenting by clicking on various parts of the TreeControlFrame. When you click anywhere inside of the frame, you should see the output mouseClicked() called appear on the DOS screen. This is how I first began to understand more about the mouseClicked event.
To test another TreeControl events, close the application, and then switch back to the Design tab of the AppBrowser, so you can see the Inspector frame. Select another event in the Event tab group, such as the selectionChanged event, and double-click in it's text field. With this double-click you'll see the name of the method appear in the text field. Double-click again and you're taken to the newly created treeControl1_selectionChanged() method. Add a print statement like the following to this method, and then run the application again:
Making your components behave better
By using this testing method I began to become comfortable with the various user interface events of the TreeControl and other user interface components. Over time, I've used this increased knowledge to add more customization features to my user interface components.
After some experimenting like this, I searched through the JBuilder subdirectories looking for examples of the selectionChanged event. In the JBuilder's sample subdirectories, I found some useful sample code, and changed my selectionChanged() method to include the following lines of code:
Conclusion
I hope this simple method works as well for you as it has for me.
Whenever I find that I don't understand the behavior of an event, I use
this method to test the event behavior until I understand it fully.
I've found it to be a good way to easily learn more about the behavior
of user interface components.
Copyright © 1998-2005 DevDaily Interactive, Inc.
All Rights Reserved.