Building a Java text editor
JBuilder Foundation users skip Step 4 and 5, and go to Step 6. Ignore any directions in the rest of this tutorial pertaining to the Edit|Font menu item or
FontChooser
dialog.
FontChooser
dialog.
First, you need to add a FontChooser
dialog to your TextEditFrame
class before it can be used by the menu item:
TextEditFrame.java
in the UI designer.
FontChooser
FontChooser
to your design. This places the component into the class as fontChooser1
and displays it in the Other folder of the component tree.
frame
property on this dialog component for it to work properly at runtime. The frame
property must reference a java.awt.Frame
, or descendant, before being shown. In this case, the frame you need to reference is 'this' frame (TextEditFrame
). If you fail to do this, the dialog will not show, and an error message occurs at runtime. You can also set the title
property so the dialog will have an appropriate caption.
To set the frame
and title
properties,
fontChooser1
in the Other folder of the component tree and click the frame
property value in the Inspector.
this
from the list of values.
title
property value, and type the word Font
as its value.
As a result, the following lines are added to the source code in the jbInit()
method:
fontChooser1.setFrame(this); fontChooser1.setTitle("Font");Placing the
FontChooser
into the component tree and setting these properties creates code in your class that instantiates a FontChooser
dialog for your class, sets its title
to "Font", and sets its frame
to this
. But this code won't display the dialog or make use of it in any way. That has to be done in the "event handler" for the Edit|Font menu item. Let's create that code now.
FontChooser
:
jMenuItem5
(under the second menu node called jMenu1
.) Notice that the text
property for this menu item in the Inspector says "Font". (Don't worry if your Font menu item is a different number than this. Just make sure you select the one for the Font menu.)
actionPerformed
event.
For menus, buttons, and many other Java UI components, actionPerformed
is the main user event of interest, the one you should hook for responding to the user operating that menu or button.
The name of the event handling method appears in the value field. If the method doesn't already exist, this will show the proposed default name for a new event handling method. For this new event handler, the suggested name is jMenuItem5_actionPerformed
.
When an event handling method is new, double-clicking it in the Inspector generates an empty stub for the method in your source code. Regardless of whether the method is new or already exists, the window focus will switch to the source code in the editor and position your cursor inside the event handling method. For a new event handling method, as is the case here, you will see that there is no code yet in the body of the method.
fontChooser1.showDialog();Your method should now look like this:
void jMenuItem5_actionPerformed(ActionEvent e) { fontChooser1.showDialog(); }
FontChooser
dialog. If not, check that you set its frame
property to this
.
Nothing will happen yet if you try to change the font. This is because you aren't using the results from the FontChooser
to change the text in the text area. Let's do that next.