Connecting Tech Pros Worldwide Forums | Help | Site Map

Java GUI, take input from JText class

Newbie
 
Join Date: Oct 2006
Posts: 11
#1: Oct 19 '06
Ok. So, I don't know which class from javax.swing to use for this. I think it's JTextField, but anyway...

I'm looking for a class that allows the user to type in, in this case, integer or double values, which I'll be able to take as a string, change to type I need, then perform operations with them.

Example: 3 text fields. Miles, Gallons, and MPG. If the user puts in info for Miles, and Gallons, I should be able to take the user input, do my operation, then display the result (value for MPG) in the MPG text box.

If it's the JTextField class, I don't know which methods to use for reading the input in the box or the method to display new text in the box after its initial construction.

If anyone can help, it would be greatly appreciated.

Newbie
 
Join Date: Oct 2006
Posts: 11
#2: Oct 19 '06

re: Java GUI, take input from JText class


Omg. I feel so stupid....

.getText() and .setText().... I couldn't find them in the API because they were inherited methods. Omg... Sorry guys, scratch that question. Hahaha.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Oct 19 '06

re: Java GUI, take input from JText class


Quote:

Originally Posted by Koji

Ok. So, I don't know which class from javax.swing to use for this. I think it's JTextField, but anyway...

I'm looking for a class that allows the user to type in, in this case, integer or double values, which I'll be able to take as a string, change to type I need, then perform operations with them.

Example: 3 text fields. Miles, Gallons, and MPG. If the user puts in info for Miles, and Gallons, I should be able to take the user input, do my operation, then display the result (value for MPG) in the MPG text box.

If it's the JTextField class, I don't know which methods to use for reading the input in the box or the method to display new text in the box after its initial construction.

If anyone can help, it would be greatly appreciated.

For input, use JTextfield. To get the data from the textfield use field.getText()

For output do not use a textfield. You just want to display the data so use a JLabel. You can have its initial text to "" (nothing) then when your calculation is complete, use label.setText(result); where result is a string represantation of your result.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 19 '06

re: Java GUI, take input from JText class


Quote:

Originally Posted by Koji

Omg. I feel so stupid....

.getText() and .setText().... I couldn't find them in the API because they were inherited methods. Omg... Sorry guys, scratch that question. Hahaha.

Guess I was just a bit late then!
Newbie
 
Join Date: Oct 2006
Posts: 11
#5: Oct 19 '06

re: Java GUI, take input from JText class


Thanks for replying though! Oh, and my homework requires that we display the answers in the TextField for the missing information, not in a JLabel. :D

Thanks for your delayed help, hehe.
Reply