File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextfield refresh in keypressed event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextfield refresh in keypressed event" Watch "JTextfield refresh in keypressed event" New topic
Author

JTextfield refresh in keypressed event

Jason Bargas
Greenhorn

Joined: Jul 18, 2011
Posts: 2
Hi! Just new here in coderanch...
I'm having problems with my code regarding the value of the string I get with the JTextfield.getText() method
my code goes this way:

(keypressedevent evt){
int id=evt.getId();
char c = evt.getKeyChar;
String s = JTextField.getText()+c;
System.out.print(s);
}
if the textfield contains for example: apple
and you type s, it will print apples.
but if you pressed backspace, it would still print apples
it wouldn't update until you press another key.
Is there a way to get the desired output here?










}
jandries Aldum
Greenhorn

Joined: Jul 18, 2011
Posts: 7
This method will get the text of the textfield before the pressed key had any influence on the text field. (e.g. it will get apples before the backspace key had its effect of removing the "s")

Put your whole method in the keyTYPED action listener and not the key PRESSED.
It should work then.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Welcome to the Ranch!

Perhaps you should use a DocumentListener, attached to the text fields' Document instead. A KeyListener is triggered before the text is modified, a DocumentListener afterwards.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Bargas
Greenhorn

Joined: Jul 18, 2011
Posts: 2
The DocumentListener solution seems nice but I'm not familiar with it.
Anyways, I found a solution already. Rather than implementing my method in the keypressed or keytyped event, I used the keyreleased event, so that I could get the exact text inside the textfield.
Thanks for everything guys!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

A DocumentListener is added to the Document of a text field (or text area, or any other text component). This Document is what contains the actual String contents, and is returned by calling getDocument().

The DocumentListener listens for three events: text added, text removed, and text replaced. There are three matching methods for that.
The advantage of a DocumentListener over a KeyListener is that it listens to all changes to the Document; not just by the user typing, but also any call to setText.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JTextfield refresh in keypressed event
 
Similar Threads
Tokens
java classes
The method parse(String) from the type DateFormat is not static
Format number string to include leading zeros
Expression to get Tomcat pid?