• Post Reply Bookmark Topic Watch Topic
  • New Topic

Wait for input from JTextField before continuing with code  RSS feed

 
Christopher vanPutten
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I would like to wait for the enter key to be pressed in a jtextfield before the rest of the code continues... same as if you were doing a System.in.read(). I have everything written up... the gui, the process, the key listener on VK_ENTER... but the process just keeps going instead of waiting till I push enter. I've been playing with it for several hours now and can't figure it out. Any ideas on how to make the jtextfield act as if it were the actual console to wait for enter? Thanks!!
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christopher, I don't know if this is the sort of answer you are looking for, but the fact that you have this kind of problem suggests to me that there are higher level problems with a proper event driven program design. Do you see what I mean? Having said that, maybe there is a reason why you are doing things this way, I don't know.

Anyways, like I said, sorry of that is not the sort of answer you are looking for, perhaps someone else will be able to respond to your specific request.


edit: anyways, instead of looking for a way to have code "wait" for a return key on a JTextField, rejig your program so that rather than the code waiting for the return key, have the return key initiate the code.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the simple way is to use a JOptionPane.showInputDialog(..) in place of your textfield,
but you'll need to handle cancel/esc/X which will return null.

perhaps the 'rest of the code continues...' needs to be in a separate method, called by the textfield's actionListener (ENTER)
 
Christopher vanPutten
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I got it... simple thing like making a global boolean value and if keypressed enter... make it true..... at my statment i wanted it to wait at... while (!bool) do this... so when enter is pressed it breaks out and continues.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:the simple way is to use a JOptionPane.showInputDialog(..) in place of your textfield,
but you'll need to handle cancel/esc/X which will return null.

perhaps the 'rest of the code continues...' needs to be in a separate method, called by the textfield's actionListener (ENTER)


hmm hadn't thought of that, but it makes sense. No need for significant design change which is probably a good thing. Also the new "input field" is then not a permanent visual part of the GUI, and the old JTextField might not be needed.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher vanPutten wrote:I think I got it... simple thing like making a global boolean value and if keypressed enter... make it true..... at my statment i wanted it to wait at... while (!bool) do this... so when enter is pressed it breaks out and continues.

Does not allow for pasting in text, so I think Michael's idea best. Alternatively if you absolutely need to use a JTextField, you could use a DocumentListener, I suppose.

If further problems, perhaps you could post your code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!