Hello,

This is my first post so I hope this is clear and in the correct place.

I am having trouble dealing with the traverse method of Custom Item in J2me.

What I want to do is have two custom items, one is "start game", the other "instructions" as menu options. If the user presses OK depending on the highlighted custom item, the user will be directed to the relevant Form.

The problem is, that when I change focus to one and select OK, and then return back to the main form, the while loop doesn't execute, meaning I can't change the focus selection. I also think a while loop is poor coding practise, so any other alternative would be grealty helpful.

I hope this makes sense.

Thanks
LaChavvy

Below is the main class code:

MIDLET

package gamePackage;

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

public class Main extends MIDlet implements CommandListener
{
    private Display display;
    
    //Setup Forms
    private Form mainMenu;
    private Form gameSetup;
    private Form instructions;
    private Form rollDice;

    //Instantiate mainMenu Variables
    private int selection;
    private mainMenuCustomItem instructionCI;
    private mainMenuCustomItem startGameCI;
    
    //Instantiate gameSetup Variables
    private String gamerTag;
    private String masterPhone;

    //Instantiate rollDice Variables

    //Setup Main Menu Form
    public Main()
    {
        //Default menu selection is 1
        display = Display.getDisplay(this);
        mainMenu = new Form("Snakes and Ladders : Menu");
        startGameCI = new mainMenuCustomItem("Start Game");
        instructionCI = new mainMenuCustomItem("Instructions");

        mainMenu.append(startGameCI);
        mainMenu.append(instructionCI);

        mainMenu.addCommand(new Command("Exit", Command.EXIT, 0));
        mainMenu.addCommand(new Command("Select", Command.OK, 0));
        mainMenu.setCommandListener(this);
    }

    //Display Main Menu
    public void startApp()
    {
        display.setCurrent(mainMenu);
        checkTraversing();
    }
    public void checkTraversing()
    {
        startGameCI.setSelection(1);
        while(display.getCurrent() == mainMenu)
        {
            selection = startGameCI.getSelection();
            System.out.println(selection);
        }
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }
    
    public void commandAction(Command c, Displayable s)
    {
        //Menu EXIT
        if((s.getTitle().equals("Snakes and Ladders : Menu")) && (c.getCommandType() == Command.EXIT))
        {
            notifyDestroyed();
        }
        //Menu OK -- GAME SETUP
        if((s.getTitle().equals("Snakes and Ladders : Menu")) && (c.getCommandType() == Command.OK) && (selection == 1))
        {
            displayGameSetup();
            display.setCurrent(gameSetup);
            System.out.println(display.getCurrent());
        }
        //Menu OK -- INSTRUCTIONS
        if((s.getTitle().equals("Snakes and Ladders : Menu")) && (c.getCommandType() == Command.OK) && (selection == 2))
        {
            displayInstructions();
            display.setCurrent(instructions);
            System.out.println(display.getCurrent());
        }
        //Game Setup EXIT
        if((s.getTitle().equals("Snakes and Ladders : Game Setup") && (c.getCommandType() == Command.EXIT)))
        {
            display.setCurrent(mainMenu);
            checkTraversing();
            System.out.println(display.getCurrent());
        }
        //Instructions EXIT
        if((s.getTitle().equals("Snakes and Ladders : Instructions") && (c.getCommandType() == Command.EXIT)))
        {
            display.setCurrent(mainMenu);
            checkTraversing();
            System.out.println(display.getCurrent());
        }
        
    }
    public void displayGameSetup()
    {
        gameSetup = new Form("Snakes and Ladders : Game Setup");
        gameSetup.addCommand(new Command("Exit", Command.EXIT, 0));
        gameSetup.addCommand(new Command("Select", Command.OK, 0));
        gameSetup.setCommandListener(this);

    }
    public void displayInstructions()
    {
        instructions = new Form("Snakes and Ladders : Instructions");
        instructions.addCommand(new Command("Exit", Command.EXIT, 0));
        instructions.addCommand(new Command("Select", Command.OK, 0));
        instructions.setCommandListener(this);
    }
}

MAINSTARTGAME CUSTOM ITEM CODE

package gamePackage;

import javax.microedition.lcdui.*;

public class mainStartGame extends CustomItem {

     private boolean in; //is the item in focus?
     int selectedOption = 1;

    public mainStartGame(String title) {
        super(title);
    }

    public int getMinContentWidth() {
        return 200;
    }

    public int getMinContentHeight() {
        return 6;
    }

    public int getPrefContentWidth(int width) {
        return getMinContentWidth();
    }

    public int getPrefContentHeight(int height) {
        return getMinContentHeight();
    }
    public int getSelection()
    {
        return selectedOption;
    }
    public void setSelection(int input)
    {
        selectedOption = input;
    }

    public boolean traverse(int dir, int viewportWidth, int viewportHeight, int[] visRect_inout)
    {
        if (!in)
        {
              in = true;
        }
        else
        {
            switch (dir)
            {
                case Canvas.UP:
                selectedOption = 1;
                break;

                case Canvas.DOWN:
                selectedOption = 2;
                return false;
            }
            repaint();
        }
        return true;
    }
    
    public void traverseOut()
    {
        in = false;
    }
    public void paint(Graphics g, int w, int h)
    {
    }
}

The other custom item code is the exact same with different custom item name - mainInstructions.

Ahh, finally I find time to have look into this and you marked it as solved. Would you mind then share solution with others?

I instead opted for a canvas based MIDlet as I couldn't get it to work. The canvas way works fine so I'll be sticking with this.

Have look here I played around with CustomItem few months back. It is not bullet proof solution, but one on which you can build

I appreciate your time and effort, it's given me some help, but after working on the Canvas method to work, I feel its the best approach.

Thanks again