Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting value of jtext field

Status
Not open for further replies.

papaboy2

MIS
Jan 28, 2003
52
0
0
PH
hi, i would like to ask how can i get the value of my text field and assign it to a double variable? thank a lot!
 
Welp assume you have:
Code:
JTextField msg = new JTextField();

And an action listner listening to it:
Code:
ActionListner listener = new ActionListener()
{
  public void ActionPerformed()
  {
     String s = msg.getText();
     float f;
     try{
        f = Float.parseFloat(s);
     }catch(NumberFormatException e){
  [b]...[/b]
     }
  }
}

I was making a float of the string, but this should work, now you'd also want to force the user to only type valid numaric chars and the decimal point by implementing other actionlistners.
 
ok thanks very much, just a follow up question how can i assign a double variable to a text field? i received an error when i code the ff., just starting to use java :(

sampleText.setText(DoubleVariable);
 
Is it an object Double, or a primitive type double?
If it is an object, you can use
Code:
sampleText.setText("" + DoubleVariable.doubleValue()); // cast to a string
Otherwise, just use
Code:
sampleText.setText("" + DoubleVariable);

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top