Results 1 to 4 of 4
  1. #1
    Weeman5872 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default 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:

    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());
            }
    Any help would be appreciated, many thanks. :)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Try the keyReleased(KeyEvent e) method instead of the keyPressed method !

  3. #3
    Weeman5872 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    I could have sworn I used keyReleased the first time; which is why I went to keyPressed. It works fine now, thanks muchly!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. [SOLVED] How to get text area to line up with label.
    By Nicholas Jordan in forum AWT / Swing
    Replies: 8
    Last Post: 12-28-2010, 01:21 AM
  2. Replies: 14
    Last Post: 09-18-2009, 05:48 AM
  3. Replies: 9
    Last Post: 07-21-2009, 03:41 AM
  4. Capture speech and convert it to text in a textbox
    By Cyber Dragon in forum Advanced Java
    Replies: 1
    Last Post: 04-25-2009, 05:55 PM
  5. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •