aspose file tools*
The moose likes Java in General and the fly likes how to take the entire input string from a jtextfield ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to take the entire input string from a jtextfield ?" Watch "how to take the entire input string from a jtextfield ?" New topic
Author

how to take the entire input string from a jtextfield ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

hi , i have a problem , the problem is
i m taking an input from the user in a method
but there's a problem , when user enter "a" in the jtextfield , and i print the string then the result is "nothing" or blank or ""
when user enter ap then the result is "a"
but i want both "ap"



but this also not working , can any one has a better idea ? to take the complete string
say when user typed "a" we should get "a"
& when he typed "p" we should get "ap" and so on ..


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Aj Maven
Greenhorn

Joined: Jul 04, 2011
Posts: 22

I think you can simply get the string by calling the getText() of JTextField class


Thanks
-Aj
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Hi Naved,

I am not completely sure about the problem that you are facing(with JtextField) but I can take a guess.

1) I see that you are using keyTyped(keyEvent e) to capture any key typed, right? In this case, inside the event handler are you modifying the text field? If yes, don't do it!
2) Based on my work in Adobe's Flex, event handlers are generally asynchronous, so each time you are typing a key, keyTyped event may be called and you might be losing previous state. In this case, you have to think of synchronizing the state of the textfield yourself.

Do let me know if this has helped.

Thanks.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

@Barney - It's interesting to know about asynchronous event handlers in Flex. In Java, the event handlers are not only synchronous, but all of them execute in a single thread (which basically means that events are handled one by one, in a queue).

@Naved - I think the problem you are facing is that the event handler is being called before the model of JTextField is updated. Can you post some code (specifically, the listener)? Usually, in case of such issues, we try to insert the typed character at the point where Caret is. This insertion may be on an in-memory string (which you get by calling getText() on the text-field), or may be on the text-field's model itself, in case you want to modify the model yourself. Seeing your listener code will provide more information about what you're trying to achieve.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

Aditya Jha wrote:@Barney - It's interesting to know about asynchronous event handlers in Flex. In Java, the event handlers are not only synchronous, but all of them execute in a single thread (which basically means that events are handled one by one, in a queue).

@Naved - I think the problem you are facing is that the event handler is being called before the model of JTextField is updated. Can you post some code (specifically, the listener)? Usually, in case of such issues, we try to insert the typed character at the point where Caret is. This insertion may be on an in-memory string (which you get by calling getText() on the text-field), or may be on the text-field's model itself, in case you want to modify the model yourself. Seeing your listener code will provide more information about what you're trying to achieve.


what i m trying to do is , just validating the input for any special characters which are not needed

but let me give you one quick demo
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

If your validation can be done on a char-by-char basis (something like "only digits are allowed"), then I suggest you take the typed character from the KeyEvent object (how to get typed character can be found here).

However, if your validation has to be done against the entire String, and validating just the typed character is not sufficient, then I would again suggest you do something like:

Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
Aditya, let me elaborate on what I mean by asynchronous. Maybe you and I are talking about the same thing here.

Lets say you have 2 event handlers, handleEvent1 and handleEvent2 which will be performing task1 and task2 respectively. You can call them one after the other and the code works perfectly. As they are asynchronous, task1 will take its own time and finish and will have no bearing on task2 which will run in a separate thread. The problem occurs when task2 is dependent on task1, say an output from task1 had to be fed into task2. In this case, you as a programmer have to take care of synchronization between these 2 event calls.

In a very crude way(i think), what I do is include the call of handlerEvent2 within handlerEvent1

You can read about it here flex events

Regards.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Thanks Barney!

So, you meant multi-threaded event listeners, when you mentioned asynchronous. In any case, it is somewhat like Java, because in Java, we cannot predict (nor rely upon) the order in which multiple listeners will be called one after the another. However, the difference in Java is that both the listeners (and for that matter, ALL listeners of ALL UI events) run on the same thread, which basically means that if one performs a time-taking operation in an event method, the processing of other events (even UI painting) will be blocked (and the user will feel like the application is frozen).

For this reason, we delegate time-taking tasks to a different thread. But we need to write code to do this. There are some helper classes like SwingWorker which can make this work easier, though.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

If you want to prevent your text field from accepting certain characters, then you should use a DocumentListener. Not only is trying to do that by listening to key events difficult to manage (as you are finding out), you have probably forgotten that people can use copy and paste to input data to text fields as well as typing text on a keyboard. So after you get your key event code working, you still haven't taken care of the Control-V situation.

Here's the tutorial: How to Write a Document Listener.

Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Thanks Paul! I was about to write the same thing, with an example. But I think the link you gave is good as well.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Yes, I have found the Oracle tutorials to be of higher quality than most of the other Java tutorials you can find on the internet. And they are easy to find, too, just google "Java X tutorial" and you should find Oracle's tutorial about X in Java almost immediately.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692


}

// Validate text now
[/code]

and thanks for the code ,
@paul , thanks for control+v thing , i just forgot about that .
thanks guyz
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

naved momin wrote:[/code]

The last substring does not give you the typed char, rather it will give you an empty string if you are typing at the end of the text-field contents.

It becomes important to have this second substring when you consider the possibility of user typing in between of text-field content. the idea is to replace the current selection with the typed char. The selection could be anywhere - start of the string, middle of string or end of the string.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to take the entire input string from a jtextfield ?