• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

JTextField

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can use txtField.getText()
 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Are you okay? You look a little big. Maybe this tiny ad will help:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic