You Are Here:

Community: Developer Discussion Boards

#1 Old getting TextBox.text into variable - 2004-01-18, 15:11

Join Date: Nov 2003
Posts: 11
monkforte
Offline
Registered User
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
}
}
}
Reply With Quote

#2 Old 2004-01-18, 15:25

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
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 
    }
  }
}
shmoove
Reply With Quote

#3 Old 2004-01-18, 15:31

Join Date: Mar 2003
Posts: 33
niekvs
Offline
Registered User
... and get "Java for dummies" ;)
Reply With Quote

#4 Old 2004-01-18, 16:14

Join Date: Nov 2003
Posts: 11
monkforte
Offline
Registered User
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 :)
Reply With Quote

#5 Old 2004-01-18, 16:24

Join Date: Mar 2003
Posts: 33
niekvs
Offline
Registered User
you should probably download the API docs for J2ME from java.sun.com. There, you can find information about all classes and methods.
Reply With Quote

#6 Old 2004-01-18, 16:49

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
Any J2ME SDK you download will usually have a docs subdirectory with all the API documentation.

shmoove
Reply With Quote

#7 Old 2004-01-18, 17:00

Join Date: Nov 2003
Posts: 11
monkforte
Offline
Registered User
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();
}

}
}
Reply With Quote

#8 Old 2004-01-18, 17:34

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
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);
}
Again, a quick browse through the docs and you would've discovered it by yourself.

shmoove
Reply With Quote

#9 Old 2004-01-18, 18:57

Join Date: Nov 2003
Posts: 11
monkforte
Offline
Registered User
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
Reply With Quote

#10 Old 2004-01-18, 19:16

Join Date: Mar 2003
Posts: 33
niekvs
Offline
Registered User
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
Reply With Quote

#11 Old 2004-01-18, 20:17

Join Date: Nov 2003
Posts: 11
monkforte
Offline
Registered User
now it works, god bless you. this way it was also more educative and exciting than before :-)
Reply With Quote

#12 Old Contact Offline - 2004-10-01, 10:28

Join Date: Nov 2003
Posts: 90
cwfvanoordt
Offline
Regular Contributor
shmoove, can we talk offline, need some advice and have something that might be of interest to you.

Chris
cwfvanoordt@yahoo.co.uk
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia