File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java Micro Edition and the fly likes Basic J2ME Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Basic J2ME Question" Watch "Basic J2ME Question" New topic

Basic J2ME Question

Reinhardt Christiansen

Joined: Nov 08, 2004
Posts: 10
I am trying to understand the proper technique for dismissing a form when I am done with it.

My MIDlet, whose main class is MemberManager, displays a list of members of an organization; the user can scroll this List. In addition, it provides Commands for Adding new members, Changing data for members, and Deleting members. When they choose to Add a member, a Form containing input text fields is displayed; the user enters data into those fields and presses one of two buttons: Insert or Cancel. Insert means the user wants to add the data to the record store and Cancel means that the user has changed his mind and wants to return to the member list without adding the new data.

In my code, the logic for that displays the Add form and handles its two
commands are in a separate class, MemberAddForm. In the button handling logic in MemberAddForm, the logic for the Cancel button simply says to display an information Alert that says "Insert cancelled" for 5000 milliseconds, then return, i.e. pass control back to the class that invokes MemberAddForm, namely MemberManager. At least, that is what I would LIKE to happen ;-) Here is the actual code:

if (label.equals(CANCEL_INSERT_TEXT)) {
System.out.println(CLASS_NAME + "." + METHOD_NAME + " - User chose " +
String msg = "Insert Cancelled.";
Alert cancelled = new Alert("Action", msg, null, AlertType.INFO);
cancelled.setTimeout(5000); //milliseconds

This code works but after the return is executed, I am still on the Form
that is displayed by the MemberAddForm class, not the newly refreshed
memberList from the MemberManager class. What do I need to do to end the MemberAddForm gracefully and get back to the member list in the
MemberManager class? I'm looking for something like the dispose() method that I use when I write dialogs in J2SE.

My main class, MemberManager class, extends MIDlet. My supporting class,
MemberAddForm, extends Form. Is that the right approach? I wondered if I should have MemberAddForm extend MIDlet and then have MemberManager instantiate it but the API for MIDlet says MIDlets should never attempt to create other MIDlets. I'm a little confused because my approach in J2SE would be to make MemberAddForm a dialog that is initiated by a Frame but J2ME doesn't seem to have the concept of a

Can anyone shed some light on this for me?
Michael Yuan
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
You can require the user to dismiss the "cancelled" alert box. And in that "dismiss" command callback, you can set the display to your new screen.

Seam Framework:
Tomas Conway
Ranch Hand

Joined: Nov 02, 2004
Posts: 45
You can specify the Displayable you want to go to after the Alert is dismissed by providing a second parameter in 'Display.setCurrent()'

For example if you pass a MemberManager reference to MemberAddForm (call it 'memberManager' for example), then you can use the following line to show the alert for the specified time, and then control will pass to MemberManager.

Look up the two parameter version of Display.setCurrent() in the API.
Reinhardt Christiansen

Joined: Nov 08, 2004
Posts: 10
Thank you both very much for your reply! (Also, forgive my delay in responding: I had problems logging back in until I discovered that I had originally posted in Opera; no wonder IE wouldn't let me log in since it couldn't find my login cookie ;-)

Tomas, I used your suggestion and it worked very well. Michael, I wanted to try your approach but I couldn't quite figure out how the code would look. Could you possible give me a code fragment that demonstrates your callback technique? I'd really like to know your technique too.
I agree. Here's the link:
subject: Basic J2ME Question
It's not a secret anymore!