| Reply | « Previous Thread | Next Thread » |
|
SORRY but how do i get the string entered in a textbox into a var?
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class DeriveMIDlet extends MIDlet implements CommandListener { private Command runCommand; private Command exitCommand; private Display display; public DeriveMIDlet() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.EXIT, 1); runCommand = new Command("Run", Command.SCREEN, 2); } public void startApp() { TextBox input = new TextBox("app","", 256, 0); input.addCommand(runCommand); input.setCommandListener(this); display.setCurrent(input); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if (c == runCommand) { String function = input.text(); // -> HERE i need the input of the first text box in a variable } } } |
|
Declare the TextBox as a class member, instead of inside startApp(), and then you use TextBox.getString() to get the string:
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class DeriveMIDlet extends MIDlet implements CommandListener {
private Command runCommand;
private Command exitCommand;
private Display display;
// declare the TextBox out here:
TextBox input;
public DeriveMIDlet() {
display = Display.getDisplay(this);
exitCommand = new Command("Exit", Command.EXIT, 1);
runCommand = new Command("Run", Command.SCREEN, 2);
}
public void startApp() {
// instead of declaring it here
//TextBox input = new TextBox("app","", 256, 0);
input = new TextBox("app","", 256, 0);
input.addCommand(runCommand);
input.setCommandListener(this);
display.setCurrent(input);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if (c == runCommand) {
// since input is now a class member field you can access
// it from any member function
String text = input.getString();
System.out.println(text); // or whatever you want to do with it
}
}
}
|
|
... and get "Java for dummies" ;)
|
|
thanx
dont need java 4 dummies, but j2me for dummies ;) but couldnt find a proper documentation/tutorial... at sun website, there seem to be various articles about several tools etc., but no single document providing a complete reference including things like methods for textbox as this getstring thing. if somebody had a good document, i wouldnt have to ask that stpid questions :) |
|
you should probably download the API docs for J2ME from java.sun.com. There, you can find information about all classes and methods.
|
|
Any J2ME SDK you download will usually have a docs subdirectory with all the API documentation.
shmoove |
|
thanks again. if you answer the following question, im completely satisfied for the moment:
why doesnt hitting the run command button in the following code force the prog to display the form Form (displayResult procedure) import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class DeriveMIDlet extends MIDlet implements CommandListener { private Display display; private Command runCommand; private Command exitCommand; TextField input; public DeriveMIDlet() { exitCommand = new Command("Exit", Command.EXIT, 1); runCommand = new Command("Run", Command.SCREEN, 2); } public void startApp() { display = Display.getDisplay(this); Form main = new Form("Derivat0r"); TextField input = new TextField("enter function 2 derive","2*(3*x+x^2)^2", 256, 0); main.append(input); main.addCommand(runCommand); display.setCurrent(main); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void displayResult() { display = Display.getDisplay(this); Form form = new Form("working..."); TextField output = new TextField("result",Ableitung.ableiten(input.getString()),256,0); form.append(output); display.setCurrent(form); } public void commandAction(Command c, Displayable s) { if (c == runCommand) { displayResult(); } if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } } } |
|
Because you didn't set a listener for the commands:
Code:
public void startApp() {
display = Display.getDisplay(this);
Form main = new Form("Derivat0r");
TextField input = new TextField("enter function 2 derive","2*(3*x+x^2)^2", 256, 0);
main.append(input);
main.addCommand(runCommand);
// you're missing this line:
main.setCommandListener(this);
display.setCurrent(main);
}
shmoove |
|
what should i have searched for? if you dont know what exactly your problem is like, you also cant search for it.
now, everythings extremely fine EXCEPT for one thing: every time I perform "String function = input.getString();" my emulator bashes me by throwing some NullPointerException... TextField "input" should contain a string, so im confused once more |
|
Looks like you messed up. You know, you really should learn Java first, because if you don't, you'll keep running into problems like this.
I'll give you a hint this time, instead of a solution :) Look for all the differences between the code in your last posting (the one you started with "thanks again. if you answer the following question, im completely satisfied for the moment", and the code that schmoove gave you. You should find the answer to your question then... Good luck! -- Niek |
|
now it works, god bless you. this way it was also more educative and exciting than before :-)
|
|
shmoove, can we talk offline, need some advice and have something that might be of interest to you.
Chris cwfvanoordt@yahoo.co.uk |
| cwfvanoordt |
| View Public Profile |
| Find all posts by cwfvanoordt |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|