Results 1 to 2 of 2
  1. #1
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Key events and key listeners

    Hello again folks. Having trouble with this one. I want to update my JTextArea when a key is presses. Here's a snippet of my code:

    Java Code:
        private JTextField myText;
    
        public MyGui()
        {
            ad = new KeyWatcher();
            setFocusable(true);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myText = new JTextField(20);
            myText.addKeyListener(ad);
      
    		private class KeyWatcher extends KeyAdapter
        {
            @Override
            public void keyPressed(KeyEvent k)
            {
               {
                myText.setText(k.getKeyChar()+"");
                System.out.println(myText);
               }
            }
        }
    }
    Nothing happens when I run it. I want it to print out the value of the key(s) I've pressed. I've gone over the API about a million times but can't see where I'm going wrong. Can you help?

    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: Key events and key listeners

    You're not showing the relevant code, i.e. where do you add your JTextField to your JFrame?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Events and listeners - reduce coupling
    By Serpenthelm in forum AWT / Swing
    Replies: 6
    Last Post: 01-03-2012, 02:07 AM
  2. Listeners and events
    By cglacet in forum New To Java
    Replies: 3
    Last Post: 01-20-2011, 03:21 PM
  3. key listeners
    By zjames in forum New To Java
    Replies: 22
    Last Post: 11-24-2010, 06:58 AM
  4. Creating a GUI events with action listeners
    By sidd0123 in forum AWT / Swing
    Replies: 8
    Last Post: 04-03-2010, 12:32 AM
  5. typed events vs untyped events.
    By Drun in forum SWT / JFace
    Replies: 0
    Last Post: 11-23-2009, 01:22 PM

Posting Permissions

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