One way to implement such chat application is by using the standard Form as the main GUI class, and reallocate string items as the new messages are sent or received. This way you don't have to do anything with the TextField.
The other way is to use the TextBox and the Canvas. In this case you draw the GUI totally by youself, and when user presses some buttons (let's say keys from 0 to 9, or FIRE), you call the Display.setCurrent(textBox) method.
After that the text box appears on the screen, so user may enter new message. When user will press the OK button, the commandAction() method of the Canvas will be called, and in that case you should call the Display once again, and put Canvas for view: Display.setCurrent(canvas).
Here the pseudocode:
Code:
public class ChatCanvas extends ScreenCanvas implements CommandListener {
private TextBox textBox = new TextBox("Say", "", 160, TextField.ANY);
private Command OK = new Command("Ok", Command.BACK, 1);
private String message = null;
private Display display = null;
public ChatCanvas(Display display) {
this.display = display;
setFullScreenMode(true);
textBox.addCommand(OK);
textBox.setCommandListener(this);
} // end constructor
protected void showNotify() {
display.setCurrent(this);
}
protected void paint(Graphics g) {
// draw your chat GUI here
} // end paint
public void keyPressed(int keyCode) {
int action = getGameAction(keyCode);
switch (action) {
case UP:
// scroll chat
break;
case DOWN:
// scroll chat
break;
case FIRE:
activateTextBox(); // write a new message
break;
} // end switch
} // end keyPressed
public void activateTextBox() {
textBox.setString("Enter new message");
display.setCurrent(textBox);
} // end activateTextBox
private void deactivateTextBox() {
display.setCurrent(this); // the chat GUI back
// get text from text box and send it to the chatting server
} // end deactivateTextBox
public void commandAction(Command command, Displayable d) {
if ( c == OK ) {
deactivateTextBox();
}
repaint();
} // end commandAction
} // end ChatCanvas
Etc.
The last way is a tricky one, and requires to draw the GUI totally by youself, but it's a cool one. The first way is the simplest and the most portable across many mobile phones.