Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextField  RSS feed

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Just go through the below scenario and suggest me to solve it.
I developed a frame which consists of two textfields. These are of JTextField object types. It also consists of two buttons 'Next' and 'Report' of type JButton. One textfield will read a question from a .txt file and the question will be displayed on the text field. The user will enter the answer in another textfield. We will compare the answer with the expected answer and we will calculate the percentage. After entering the answer if the user clicks the 'Next' button next question will be displayed.
My problem is I am not able to retrieve the user answer from the textfield to compare it with the expected answer. I request to help me in solving this problem.

With Regards
V. Suresh Kumar
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why? You can use getText() method of the JTextField class.
Or you are using it and not able to retrieve through that?
 
Ikram Soomro
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use

String answer = "Orignal answer";
String useranswer = jTextField.getText();

boolean isCorrect = answer.equals(useranswer);

do u have this same problem or you have any other problem.

Ikram Soomro
(SCJP,SCWCD)
 
mamidipalli hemambar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u can use txtField.getText()
 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Thanks for your suggestion. I tried to retrieve the text using getText() method but I am not able to get. I forgot to mention in the earlier mail that I had tried using getText() method.
I used String userans = txtfield.getText();
but not getting. Is there any other way to do it.

With Regards
V. Suresh Kumar
 
Yogesh Kodgule
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think u are using getText() before the action of the button click is happening(Or before the text itself is entered). Logically the user should enter his answer in the text field and then hits Enter or clicks on the Next Button. In this action handling, you should retrieve the text and then proceed with other tasks.

Or this is what you r doing and still unable to get the text?

if so, then plz try to share the piece of code...!

Also I would suggest to set some trial text by using setText() and try to get this one by getText() somewhere , print on console and do some trial nd error stuff.


Yogesh Kodgule
 
Michael Tiffany
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What event are you using to indicate that the user has finished entering their answer? Are you waiting for a KeyEvent, like the user hitting the "Enter" key, or are they supposed to press a button?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!