Ok, I’m trying to make a quick double validator for any JTextField in a program. I would like to pass this to a function which then checks the contents of a field, makes sure it’s a double, then return it as a double to the function.
Here is what I have so far but theTF.getValue() should get the value of the string, not the string itself.
public double valDouble(String theTF)
{
double theDouble;
try
{
theDouble = theTF.getValue();
return theDouble;
}
}
How can I run the getValue() on the contents of the string?
Edit:
Ok, I somewhat screwed up my meaning. I understand the parseDouble, but I left it out on accident here is what I have now:
public static double valDouble(String theTF)
{
double theDouble;
try
{
theDouble = Double.parseDouble(theTF);
}
catch(NumberFormatException e3)
{
theDouble = 0;
}
return theDouble;
}
My Real issue is theTF represents the name of a TextField. I need the value OF the text field not the value in theTF. I’d like to have this here to, perhaps, highlight the field red etc.
Answer(Thanks Hunter):
The best way was to pass the Object, not sure why I was trying to parse out the value of a string reference.
public static double vDbl(JTextField theTF)
{
double theDouble;
try
{
theDouble = Double.parseDouble(theTF.getText());
}
catch(NumberFormatException e3)
{
theTF.setText("Invalid");
return 0;
}
return theDouble;
}
You should really be passing the JTextField object to your method to get the information you are looking for, I don’t even know if it’s possible to get the JTextField object from just its name; maybe with reflection, but for this application reflection seems overly complex.
ex: