Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3827
  • Last Modified:

J2ME - Auto display upon changing selection in a ChoiceGroup object

hi all,

i hope i have placed this in the right place - it's java programming afterall, only it is J2ME, not the typical J2SDK.

i've just installed the wireless tookit 2.2 from sun, and was playing around with the demo applications when i found something which is close to an application i would like to develop, only that i need to add in the details of my calculations. i'm refering to the alert demo (under UIDemo) which displays the type of alert and the corresponding delay to show the alert.

in the demo, the user has to click on the button (on the emulator) corresponding to 'show' in order for the alert (and the corresponding delay) to be displayed. however, the application i was planning to develop i hope that the alert message will be automatically displayed, rather than upon the activation of a button. i was planning to calculate the cost to take a train any between 2 stations, so whenever the user changes either of the ChoiceGroup selection, the fare should be updated and displayed automatically below the second ChoiceGroup.

anyone with any idea how to do this?
0
Aloysius Low
Asked:
Aloysius Low
  • 6
  • 5
1 Solution
 
OBCTCommented:
Just create an instance of javax.microedition.lcdui.Alert, then when you want to show the alert, set it as the currently displayed object.

E.g.

Alert alert = new Alert("My Alert");
alert.setTimeout(3000); // 3 seconds
alert.setString("My Message");

Display.getDisplay(this).setCurrent(alert); // this line of code would need to be called either from the MIDlet or a class with a reference to it
0
 
OBCTCommented:
This same code (or at least similar) is used in the UIDemo however it is being called on the event of a command button being pressed. You can call the code from where ever you want.
0
 
Aloysius LowAuthor Commented:
hi OBCT,

but won't this show up on a new screen right? also, it won't be automatic after changing an option isn't it? is there something like an onChange (like in a HTML form <input> tag) which can automatically execute this?

in UIDemo, a new AlertListener is added to the mainForm which is activated upon a button press. if i want it automatic where do i add the code?

thanks.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
OBCTCommented:
Implement the javax.microedition.lcdui.ItemStateListener interface and add it to the ChoiceGroup.
Whenever a change occurs in the ChoiceGroup, the itemStateChanged(Item) method will be called.

For example...

public class SimpleForm extends Form implements ItemStateListener
{
  private ChoiceGroup choice = new ChoiceGroup("My ChoiceGroup", Choice.IMPLICIT);

  ...

  public SimpleForm()
  {
     super("My Simple Form");

    append(choice);
    setItemStateListener(this);
  }

  public void itemStateChanged(Item src)
  {
    if (src == choice)
    {
      // make call to show Alert
    }
  }
}

Is this what you mean or have I mis-understood?
0
 
Aloysius LowAuthor Commented:
hi OBCT

you said "public class SimpleForm extends Form implements ItemStateListener" so my class don't extends MIDlet? sorry but i'm pretty new to this and still playing around with this.

thanks
0
 
Aloysius LowAuthor Commented:
hi OBCT,

with the code u provided above, i have been able to do some, close to getting what i wanted, just that the alert was not displayed. now i know which has changed, and the correct code has been executed but nothing displayed. how do i make the display of the fare to be below the 2 ChoiceGroups?

thanks
0
 
OBCTCommented:
>you said "public class SimpleForm extends Form implements ItemStateListener" so my class don't extends MIDlet?

Every J2ME application will need to extend MIDlet. In the example I gave, I extended Form just to make things a little easier to read and to separate the form from the MIDlet.
In your MIDlet, you would create an instance of SimpleForm and set it to the display.
You could of course create a normal form from your MIDlet, add the ChoiceGroup and ItemStateListener however sometimes it is nice to have things separate. (Not necessary of course)

As for displaying the fare, in your MIDlet, create a member variable of the type javax.microedition.lcdui.Display.
In the startApp() method that needs to be overridden, you would need to use something like this...

private Display display;

...

public void startApp()
{
  if (display == null)
  {
    display = Display.getDisplay(this); // 'this' being the MIDlet
  }
}

Now that you have a reference to the MIDlet's display, you can call setDisplayable(Displayable) to set any Displayable class to the screen.
In your case, this would be the Alert.
Creating the Alert is simple as I showed before, so to display to fare, you would just need to construct a String with the fare information and set that String to the Alert using the setString(String) method.

If I am still misunderstanding, perhaps you could show me the code you have written so far and we can take it from there.
0
 
Aloysius LowAuthor Commented:
hi OBCT,

here's my code:
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

public class Fare extends MIDlet {

  private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
  private final static Command CMD_SHOW = new Command("Show", Command.SCREEN, 1);

  private final static String[] originString = {"station1", "station2", "station3", "station4"};
  private final static String[] destinationString = {"station5", "station6", "station7", "station8"};
  private final static String[][] fares = new String[originString.length][destinationString.length];

  private Display display;
  private DisplayForm mainForm;
  private boolean firstTime = true;

  public Fare() {
    ChoiceGroup origin = new ChoiceGroup("Origin", ChoiceGroup.POPUP, originString, null);
    ChoiceGroup destination = new ChoiceGroup("Destination", ChoiceGroup.POPUP, destinationString, null);

    //not implemented the fare yet - to be read from text file?
    for (int i = 0; i < originString.length; i++)
      for (int j = 0; j < destinationString.length; j++)
        fares[i][j] = String.valueOf(i) + "." + String.valueOf(j) + "0";

    mainForm = new DisplayForm("Train Fare Display", origin, destination, fares);
    mainForm.addCommand(CMD_SHOW);
    mainForm.addCommand(CMD_EXIT);
    //displays the fare on a new screen on button press
//    mainForm.setCommandListener(new AlertListener(origin, destination));

  }

  protected void startApp() {
    display = Display.getDisplay(this);
    display.setCurrent(mainForm);
  }

  //class to displays the selected options on a new screen on button press
  private class AlertListener implements CommandListener {
    ChoiceGroup originCG, destinationCG;

    public AlertListener(ChoiceGroup origin, ChoiceGroup destination) {
      originCG = origin;
      destinationCG = destination;
    }

    public void commandAction(Command c, Displayable d) {
      if (c == CMD_SHOW) {
        int orgIndex = originCG.getSelectedIndex();
        int destIndex = destinationCG.getSelectedIndex();

        Alert alert = new Alert("Train Fare Information");
        alert.setType(AlertType.INFO);
        alert.setTimeout(Alert.FOREVER);
        alert.setString("Fare for train ride between " + originString[orgIndex] + " and " + destinationString[destIndex] +
          " is: $" + fares[orgIndex][destIndex]);
        display.setCurrent(alert);
      }
      else if (c == CMD_EXIT) {
        destroyApp(false);
        notifyDestroyed();
      }
    }
  }

  protected void destroyApp(boolean unconditional) {
  }

  protected void pauseApp() {
  }

  //current implementation - shows the fares on a new screen automatically
  private class DisplayForm extends Form implements ItemStateListener {
    String[][] fares;
    ChoiceGroup origin, destination;

    public DisplayForm(String title, ChoiceGroup origin, ChoiceGroup destination, String[][] fare) {
      super(title);

      this.origin = origin;
      this.destination = destination;
      this.fares = fare;

      append(origin);
      append(destination);

      setItemStateListener(this);
    }

    public void itemStateChanged(Item src) {
      //how to make it show right below the 2 choicegroups?
      int orgIndex = origin.getSelectedIndex(), destIndex = destination.getSelectedIndex();
      if (src == origin) {
        System.out.println("origin changed");
        Alert alert = new Alert("Train Fare Information");
        alert.setType(AlertType.INFO);
        alert.setTimeout(Alert.FOREVER);
        alert.setString("Fare for train ride between " + originString[orgIndex] + " and " + destinationString[destIndex] +
          " is: $" + fares[orgIndex][destIndex]);
        display.setCurrent(alert);
      }
      else if (src == destination) {
        System.out.println("destination changed");
        Alert alert = new Alert("Train Fare Information");
        alert.setType(AlertType.INFO);
        alert.setTimeout(Alert.FOREVER);
        alert.setString("Fare for train ride between " + originString[orgIndex] + " and " + destinationString[destIndex] +
          " is: $" + fares[orgIndex][destIndex]);
        display.setCurrent(alert);
      }
    }
  }
}

the current implementation will do a println on the J2ME toolkit's screen indicating that a change has been detected, but still display the message on a new screen. can i do it on the same screen?

thanks
0
 
OBCTCommented:
Ah ok I think I understand now.
You could append a javax.microedition.lcdui.StringItem to the form. A StringItem is display-only; the user cannot edit the contents.
This would be best to be instantiated from your constructor and set the text on the event of an itemStateChange.

E.g.

StringItem infoItem = new StringItem(null, null); // Create a StringItem with no title or label.
infoItem.setText("A change has occurred"); // Set the text
infoItem.setLabel("My new label"); // Set the label should you need to

The only problem with this is that if the forms content is too large for the screen, users may need to scroll up or down to see the StringItem.
Maybe using the form's ticker is an option?
0
 
Aloysius LowAuthor Commented:
hi OBCT,

thanks for the assistance. this last bit of information (together with what you had provided earlier) is just what i needed. in fact, i came up with this code fragment which had worked before you made this post, but was wondering if there were any other solutions, but since you were thinking along the same line, i guess i wasn't wrong. anyway here is the code fragment of my implementation:

    public void itemStateChanged(Item src) {
      //how to make it show right below the 2 choicegroups?
      int orgIndex = origin.getSelectedIndex(), destIndex = destination.getSelectedIndex();
      StringItem fareDisplay, orgStnDisplay, destStnDisplay;

      int cost = calculate(orgIndex, destIndex);
      if (fareIndex > -1 && orgStnIndex > -1 && destStnIndex > -1) {
        orgStnDisplay = (StringItem) mainForm.get(orgStnIndex);
        orgStnDisplay.setText(originString[orgIndex]);

        destStnDisplay = (StringItem) mainForm.get(destStnIndex);
        destStnDisplay.setText(destinationString[destIndex]);

        fareDisplay = (StringItem) mainForm.get(fareIndex);
        fareDisplay.setText("is NT$" + String.valueOf(cost));
      }
      else {
        mainForm.append("Fare for train ride between");
        orgStnIndex = mainForm.append(originString[orgIndex]);
        mainForm.append("and");
        destStnIndex = mainForm.append(destinationString[destIndex]);
        fareDisplay.setText("is NT$" + String.valueOf(cost));
      }
      display.setCurrent(mainForm);
    }
0
 
OBCTCommented:
>thanks for the assistance

Always happy to help :-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now