Loading...
Logo
Processing Forum
I recently donwloaded the GP4 tool created by Quark.
I would like to use a textField box for a user to input a latitude and longitude.
I would then use these variable for calculation so they must be in the form of a integer or double.
My first guess was to simple put:

    double lat = textField1;

but I get the error message "Cannot convert from GTextField to Double"

Any suggestions as to what I might be doing wrong?

Replies(2)

So after reading up on Objects and how they work ( http://processing.org/learning/objects/), I realized I need to use a function '.viewText()' in the GtextField class to pull that string variable from the object. So using my new found knowledge the code looks like this:
 

import guicomponents.*;

void setup(){
  size(800,600);
  createGUI();
 
}

void draw(){ 
   String txt = textfield1.viewText();
   double aInt = Double.parseDouble(txt);
  println(aInt+100.1);
}


and the gui$ file looks like this:

void textfield1_Enter1(GTextField textfield) { //_CODE_:textfield1:289526:
  println("textfield1 - GTextField event occured " + System.currentTimeMillis()%10000000 );
} //_CODE_:textfield1:289526:



// Create all the GUI controls. 
// autogenerated do not edit
void createGUI(){
  G4P.setColorScheme(this, GCScheme.BLUE_SCHEME);
  G4P.messagesEnabled(false);
  label1 = new GLabel(this, "Latitude:", 10, 30, 80, 20);
  textfield1 = new GTextField(this, "41", 130, 30, 80, 20, false);
  textfield1.addEventHandler(this, "textfield1_Enter1");
}

// Variable declarations 
// autogenerated do not edit
GLabel label1; 
GTextField textfield1; 



The only problem is that for some reason the calculation gives me the answer "141.0999984741211" while I am expecting 141.1.
It would be nice if there was some way to correct this error; however, it is such a small difference I don't think it will matter.

Thanks again to Quark for creating this tool!!!  

I see that you have solved the original problem of converting a String to a Double but I would not use viewText rather use getText i.e.
Copy code
  1. String txt = textfield1.getText();
  2. double aInt = Double.parseDouble(txt);
In your code it won't make a difference but the getText method is the correct one to use, viewText is really for use with the multiline GTextField.

The only problem is that for some reason the calculation gives me the answer "141.0999984741211" while I am expecting 141.1

The reason is that computers do not store floating point numbers exactly. Java uses 8 bytes of memory to store a double which gives 2^64 different permutations of 0s and 1s and there is no permutation that equates exactly to 141.1 so you get an accurate approximation

If you enter 0.1 you will find the same problem.

Glad you like the tool.