Results 1 to 4 of 4
- 01-16-2011, 02:08 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
Copying a text value from a textbox to a label
I have a frame with a textbox (txtInput) and a label (lblModifiedText). Currently, I have an event (keyPressed) assigned to the textbox; this is so when I type anything in the text box, it is copied to the label. However, I find the label is always one character behind. For example:
txtInput: 123
lblModifiedText: 12
Then, if I press backspace, I get the following:
txtInput: 12
lblModifiedText: 123
Backspace again:
txtInput: 1
lblModifiedText: 12
I'm using the following code at the moment:
Any help would be appreciated, many thanks. :)Java Code:String inputText = txtInput.getText(); if (rdoNoChange.isSelected() == true) { lblModifiedText.setText(inputText); } else if (rdoUpperCase.isSelected() == true) { lblModifiedText.setText(inputText.toUpperCase()); } else if (rdoLowerCase.isSelected() == true) { lblModifiedText.setText(inputText.toLowerCase()); }
- 01-16-2011, 02:14 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Try the keyReleased(KeyEvent e) method instead of the keyPressed method !
- 01-16-2011, 02:17 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 2
- Rep Power
- 0
I could have sworn I used keyReleased the first time; which is why I went to keyPressed. It works fine now, thanks muchly!
-
You shouldn't be using key listeners at all here. What happens if someone copies and pastes text into the JTextField? Or deletes text? Rather you should use a DocumentListener.
Similar Threads
-
[SOLVED] How to get text area to line up with label.
By Nicholas Jordan in forum AWT / SwingReplies: 8Last Post: 12-28-2010, 12:21 AM -
Why Buttons and label also adjust when i adjust my textbox?
By mlibot in forum New To JavaReplies: 14Last Post: 09-18-2009, 04:48 AM -
problem in displaying text inside the item label - JFREECHART
By chittora in forum Java 2DReplies: 9Last Post: 07-21-2009, 02:41 AM -
Capture speech and convert it to text in a textbox
By Cyber Dragon in forum Advanced JavaReplies: 1Last Post: 04-25-2009, 04:55 PM -
POI changing text to a form label
By natt in forum Advanced JavaReplies: 2Last Post: 09-19-2008, 10:49 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks