java-metextinputmidplcdui

How do I get input from a user using j2me canvases? is this even possible?


I am currently trying to learn J2ME and build a connect four game (some of you might know this as 'four in a row'). I've More or less got all of the aspects of my game working, apart from one thing that is driving me mad! This is of course getting the text from the user!

For the two player mode of the game I want to be able to allow each player to enter their name. I am struggling to find a working example of text input that doesn't use the main Midlet.

For example the examples on java2x.com just use a single midlet (no classes or canvases or anything).

As it stands my application's main midlet start method simply opens a main menu class:

    package midlet;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import view.*;

public class Main extends MIDlet {
    public void startApp() { 
        MainMenu mm = new MainMenu();
        showScreen(mm);
    }

    public static void showScreen(Displayable screen) {
        Display.getDisplay(instance).setCurrent(screen);
    }

    public void pauseApp() {
    }

    public static void quitApp() {
        instance.notifyDestroyed();
    }

    public void destroyApp(boolean unconditional) {
    }
}

The main menu class is as follows:

 package view;

    import javax.microedition.lcdui.*;
    import lang.*;
    import model.*;
    import midlet.Main;

    public class MainMenu extends List implements CommandListener {

        private Command ok = new Command(StringDefs.currDefs.getString("TEXT_OK"), Command.OK, 1);

        public MainMenu() {
            super(StringDefs.currDefs.getString("TEXT_TITLE"), List.IMPLICIT);
            // we we add in the menu items
            append(StringDefs.currDefs.getString("TEXT_PLAY1"), null);
            append(StringDefs.currDefs.getString("TEXT_PLAY2"), null);
            append(StringDefs.currDefs.getString("TEXT_HIGHSCORETABLE"), null);
            append(StringDefs.currDefs.getString("TEXT_HELP"), null);
            append(StringDefs.currDefs.getString("TEXT_QUIT"), null);
            this.addCommand(ok);
            this.setCommandListener(this);
        }

        public void commandAction(Command c, Displayable d) {
            if (c == ok) {
                int selectedItem = this.getSelectedIndex();
                if (selectedItem != -1) {
                    switch (selectedItem) {
                        case 0:
                            GameBoard gameBoard = new model.GameBoard();
                            GameCanvasOnePlayer board = new GameCanvasOnePlayer(gameBoard);
                            Main.showScreen(board);
                            break;
                        case 1:
                            GameBoard gameBoardTwo = new model.GameBoard();
                            GameCanvasTwoPlayer GameCanvasTwoPlayer = new GameCanvasTwoPlayer(gameBoardTwo);
                            Main.showScreen(GameCanvasTwoPlayer);
                            break;
                        case 2:
                            HighScores hc = new HighScores();
                            midlet.Main.showScreen(hc);
                            break;
                        case 3:
                            Help he = new Help();
                            midlet.Main.showScreen(he);
                            break;
                        case 4:
                            QuitConfirmation qc = new QuitConfirmation();
                            midlet.Main.showScreen(qc);
                            break 
                    }
                }
            }
        }
    }

When a two player game is selected (case 1 in the above switch) from this menu I would like two text boxes to appear so that I can get both player names and store them.

What would be the best way of going about this? is this even possible with canvases? And do you know where I can find a relevant example or at least something which may help?


Solution

  • You can either: 1. Make the user enter his input in an ugly Textbox (which takes the whole screen) 2. Use the textbox control I've written from scratch a long time ago which is available here and looks something like this (3 Textfields shown):

    alt text