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
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.
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.
Open MyBaseJPanel.java
in the GUI Editor with the Design mode selected.
To add an Event Listener:
jButton1MouseClicked
with the following code:jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText("");
MyBaseJPanel.java
to MyJFrame.java
, if MyJFrame already contains MyBaseJPanel, then remove it, and press Shift+F6(shortcut).Now it is possible to write and clear the text that was placed inside the TextFields.
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.
Unsure how to edit the code generated by the IDE?
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:
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.
52.14.76.200