Creating a new TextEditor

A TextEditor can be created by two different methods:

  • Defining the exact dimensions (width and height) of the editor.

  • Defining the number of visible rows.

The client application sets a valid parent object for the TextEditor in order to display the editor. By default, the editor is not visible and not focused when a parent is set, so the client application takes care of setting the editor visible in order for the user to see it. Additionally, the client application sets the focus to the editor so that the user can enter text to it.

When the client application does not need an input from the user, the focus can be removed from the editor by calling TextEditor.setFocus( false ).

The following example uses the constructor that takes the row count as a parameter and adds the TextEditor to Canvas and sets it visible and focused.

TextEditor is derived from CanvasItem and it is used through that interface in the example.

// TextEditorExample.java

private static final int MAX_SIZE 	= 200;
private static final int WIDTH 	= 180;
private static final int ROW_COUNT = 3;

// ...

public void startApp()
    {
    // ...

    // Create a TextEditor that is three rows high. Input constraints
    // are set to support any character and set width to 180 pizels
    TextEditor editor = TextEditor.createTextEditor( 
        MAX_SIZE, TextField.ANY, WIDTH, ROW_COUNT);
    // Set the TextEditor multiline.
    editor.setMultiline(true);

    // Create the TextEditor in the Canvas.
    canvas.addItem(editor);

    // Parent has been set in canvas.addItem() so the editor
    // can be focused safely here. The user can type text to the editor.
    editor.setFocus(true);
    
    // ...
    }

// TextEditorCanvas.java

// Some default position for the item.
private static final int ITEM_POSITION_X = 15;
private static final int ITEM_POSITION_Y = 10;

// ...

/**
 * Adds a new canvas item to this canvas.
 * 
 * @param item The item to be added.
 */
public void addItem(CanvasItem item)
    {
    // Set this canvas as a parent for the new canvas item.
    item.setParent(this);
    // Set the position for the new canvas item.
    item.setPosition(ITEM_POSITION_X, ITEM_POSITION_Y);
    // Set the canvas item visible.
    item.setVisible(true);
    }

// TextEditorExample.java