Tutorial Tutorial Step 15

Building a Java text editor

Step 14: Adding a right-click menu to the text area

This step is for JBuilder Professional and Enterprise only. Foundation users skip this step and go to Step 15.

The DBTextDataBinder component adds a right-click menu to Swing text components for performing simple editing tasks such as cutting, copying, or pasting clipboard data. DBTextDataBinder also has built-in actions to load and save files into a JTextArea, but they don't allow you to retrieve the file name loaded or saved, which you display in your status bar. For the purposes of this tutorial, we are going to add a DBTextDataBinder, bind it to jTextArea1, and suppress the file Open and Save actions.

  1. Click the Design tab and select the DBTextDataBinder component   on the dbSwing Models tab of the palette.

  2. Drop it anywhere in the designer or on the component tree. It is placed in the Data Access folder in the tree as dBTextDataBinder1.
  3. Select dBTextDataBinder1 in the component tree, and then click its jTextComponent property in the Inspector.

  4. Click the Down arrow on that property value and choose jTextArea1 from the drop-down list.

    This binds dBTextDataBinder1 to jTextArea1 by placing the following line of code in the jbInit() method.

    dBTextDataBinder1.setJTextComponent(jTextArea1);
    
  5. Select the enableFileLoading property for dBTextDataBinder1 and set its value to false using the drop-down arrow. Do the same thing for the enableFileSaving property.

  6. Save your work, then run the application. Notice that you now have a pop-up menu when you right-click the text area. Also notice that it does not contain menu items for Open and Save.

Note: You can actually add any of the items on the right-click menu to your menu bar and toolbar if you wish by using the DBTextDataBinder public static Action classes, but you would have to provide the icons and write the code manually.

    button = productsToolBar.add(DBTextDataBinder.UNDO_ACTION);
    button.setText("");
    button.setPreferredSize(buttonSize);

For an example of how to do this, see the TextPane sample in the JBuilder samples folder: jbuilder/samples/dbswing/TextPane

For more information on the DBTextDataBinder component,

  1. Drill down into the TextEditFrame component in the structure pane with TextEditFrame.java open in the editor.

  2. Select the dBTextDataBinder1 component. The code is highlighted in the editor.

  3. Right-click the highlighted code in the editor and select Browse Symbol. The DBTextDataBinder source code file opens in the editor. Click the Doc tab to view the documentation.

Close the "Text Editor" application before continuing to the next step.

Tutorial Tutorial Step 15