# J2ME question



## tovachanah (Feb 15, 2006)

attached is the code for a J2ME (straight from the book)

It doesn't work in netbeans.

2 questions

1, where do the images go? i,e. in what folder of the project and
2 why doesn't the emulator work?

code 1 TravelList
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class TravelList
extends MIDlet 
implements CommandListener {
private List mList;
private Command mExitCommand, mNextCommand;

public TravelList() {
String[] stringElements = { "Airplane", "Car", "Hotel" };
Image[] imageElements = { loadImage("/airplane.png"), 
loadImage("/car.png"), loadImage("/hotel.png") };
mList = new List("Reservation type", List.IMPLICIT,
stringElements, imageElements);
mNextCommand = new Command("Next", Command.SCREEN, 0);
mExitCommand = new Command("Exit", Command.EXIT, 0);
mList.addCommand(mNextCommand);
mList.addCommand(mExitCommand);
mList.setCommandListener(this);
}

public void startApp() {
Display.getDisplay(this).setCurrent(mList);
}

public void commandAction(Command c, Displayable s) {
if (c == mNextCommand || c == List.SELECT_COMMAND) {
int index = mList.getSelectedIndex();
Alert alert = new Alert("Your selection",
"You chose " + mList.getString(index) + ".",
null, AlertType.INFO);
Display.getDisplay(this).setCurrent(alert, mList);
}
else if (c == mExitCommand)
notifyDestroyed();
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

private Image loadImage(String name) {
Image image = null;
try {
image = Image.createImage(name);
}
catch (IOException ioe) {
System.out.println(ioe);
}

return image;
}
}

and 2 Jargoneer (no images)

import java.io.*;

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

public class Jargoneer extends MIDlet
implements CommandListener, Runnable {
private Display mDisplay;

private Command mExitCommand, mFindCommand, mCancelCommand;

private TextBox mSubmitBox;
private Form mProgressForm;
private StringItem mProgressString;

public Jargoneer() {
mExitCommand = new Command("Exit", Command.EXIT, 0);
mFindCommand = new Command("Find", Command.SCREEN, 0);
mCancelCommand = new Command("Cancel", Command.CANCEL, 0);

mSubmitBox = new TextBox("Jargoneer", "", 32, 0);
mSubmitBox.addCommand(mExitCommand);
mSubmitBox.addCommand(mFindCommand);
mSubmitBox.setCommandListener(this);

mProgressForm = new Form("Lookup progress");
mProgressString = new StringItem(null, null);
mProgressForm.append(mProgressString);
}

public void startApp() {
mDisplay = Display.getDisplay(this);

mDisplay.setCurrent(mSubmitBox);
}

public void pauseApp() {}

public void destroyApp(boolean unconditional) {}

public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if (c == mFindCommand) {
// Show the progress form.
mDisplay.setCurrent(mProgressForm);
// Kick off the thread to do the query.
Thread t = new Thread(this);
t.start();
}
}

public void run() {
String word = mSubmitBox.getString();
String definition;

try { definition = lookUp(word); }
catch (IOException ioe) {
Alert report = new Alert(
"Sorry",
"Something went wrong and that " +
"definition could not be retrieved.",
null, null);
report.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(report, mSubmitBox);
return;
}

Alert results = new Alert("Definition", definition,
null, null);
results.setTimeout(Alert.FOREVER);
mDisplay.setCurrent(results, mSubmitBox);
}

private String lookUp(String word) throws IOException {
HttpConnection hc = null;
InputStream in = null;
String definition = null;

try {
String baseURL = "http://65.215.221.148:8080/wj2/jargoneer?word=";
String url = baseURL + word;
mProgressString.setText("Connecting...");
hc = (HttpConnection)Connector.open(url);
hc.setRequestProperty("Connection", "close");
in = hc.openInputStream();

mProgressString.setText("Reading...");
int contentLength = (int)hc.getLength();
if (contentLength == -1) contentLength = 255;
byte[] raw = new byte[contentLength];
int length = in.read(raw);

// Clean up.
in.close();
hc.close();

definition = new String(raw, 0, length);
}
finally {
try {
if (in != null) in.close();
if (hc != null) hc.close();
}
catch (IOException ignored) {}
}

return definition;
}
}


----------

