Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

cant retrieve string from JTextField

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to retrieve a string that was entered by a user, but so far I am not getting anything back from my System.out.print() check.



this snipet of code does the if() test to see what came in through the user


a tutorial told me that getText() is what I should use to get the string from JTextField

I appreciate the help
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it doesn't work because the user hasn't entered any data when the statement is executed.

You use the getText() method when some event occurs. That is maybe the user clicks on a button. So you add an ActionListener to the button and then you get the text.

Read the Swing tutorial on "How to Write an Action Listener".

In fact download the tutorial and read it and look at the examples.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, I am really having a hard time understanding how this works. I have read and watched a number of different tutorials on this topic but I completely get it. I understand this maybe very easy, but I am not getting it. Please help me, I would really appreciate it.

I tried a couple different methods:



The button click I can get to work, and thats because getActionCommand() only accepts strings. I have tried switching the next test,
event.getSource() == field1
to
event.getActionCommand() == inPut1

this wont cause the action to fire though. The other method atleast caused the action to fire but it wasnt recieving the text from the user. I tried moving the
inPut1 = field1.getText();
inside the
if (event.getActionCommand().equals("OK"))
which is essentially what I had before I realize due to the method call.
obviously no help.

Could you please help me with this problem. Like I said, I would really appreciate it if you would help me on this one.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please help me with this problem.


I did give you help. In your last posting I told you to create a really really simple example called a SSCCE so you can understand the concepts.

So you create a frame with a text field and a button and add an ActionListener to the button.

Every questions should have a SSCCE that shows exactly what you are trying to do because quite frankly all your questions are extremely basic and fully covered in the tutorials and we can't guess why you are not following the code posted in the tutorials and will only waste time guessing.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isnt a question of why am I not following or understanding the tutorials... or the fact that there is not a tutorial for every problem you need help on, so you ask for help. Sometimes though, you get a helpful response of: this is easy for me, figure it out on your own. So your left guessing as to how to get it to work.

If you are not willing to help me on this, would you mind pasting a link to a tutorial that covers getting text from a JTextField upon pressing enter? All I can find is setting text to a JTextField and events upon button clicks.

Thanks for the help
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Hultin wrote:This isnt a question of why am I not following or understanding the tutorials... or the fact that there is not a tutorial for every problem you need help on, so you ask for help. Sometimes though, you get a helpful response of: this is easy for me, figure it out on your own. So your left guessing as to how to get it to work.

If you are not willing to help me on this, would you mind pasting a link to a tutorial that covers getting text from a JTextField upon pressing enter? All I can find is setting text to a JTextField and events upon button clicks.

Thanks for the help


http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

Note the part regarding adding an ActionListener to the text field. If you had really read this tutorial, I fail to see how you missed this. It's the very first demo. So obviously, you aren't following or understanding them. We're here to help. But we are not an answer mill.
 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original question stated:

I am trying to retrieve a string that was entered by a user,


As I responded, this is done when an event occurs and suggested that maybe a button click should be used to get the text.

Now you respond:

would you mind pasting a link to a tutorial that covers getting text from a JTextField upon pressing enter?


So here we have a better requirement. Again I am NOT a mind reader. Your question was not complete and clear and I DID give you the best answer possible which was to create an ActionListener.

In your last posting I asked you for a SSCCE. Instead Maneesh spoon fed you the answer. Therefore you did not learn how to ask a simple direct question and provide a simple SSCCE showing the code you used to test it.

Again with this posting you where asked for a SSCCE to confirm the context of your question. You did not provide one and I will NOT spoon feed the answer. I attempt to teach problem solving skills.

 
Esmaeil Ashrafi
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
respecting to all dear experts ;

Ben Hultin wrote:
getting text from a JTextField upon pressing enter?
Thanks for the help

you should just register the ActionListener to your JTextField if you want the event fired by pressing "Enter" key :



I wish i guessed right.But hawever the comments and the section of tutorial others linked is the complete resource

Sincerely
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you seem to be missing the concept that Swing is mostly event-driven,
something happens, something listens, something is programmed to happen based on what is heard

do you just pick up your telephone and say hello?
or do you wait for it to ring first?

so,
something happens:
user types (or not) something in the textfield, then presses enter

something listens:
can be a keyListener, also can be other listeners, but usually is an actionListener

something is programmed to happen based on what is heard:
in the actionListener's actionPerformed() you put what you want to happen, based on the user's input.
Also, can involve some validation of the input
 
machines help you to do more, but experience less. Experience this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!