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

    Default Why is my KeyAdapter acting weird? - please don't move to awt/swing

    Hello

    Sorry about the title, but my last post got moved and since hasn't had any attention, and yes, I'm new to Java so I feel this is the correct forum to post on. Anyway, onto the issue.
    Java Code:
    private class Watcher extends KeyAdapter
      @Override
      public void keyPressed(KeyEvent k)
        {
           Character c = k.getKeyChar();
           char cA = c.toUpperCase(c);
           System.out.println(cA);
         }
    When I run the code and press a key I get cA twice. What's going on there?

    Thanks in advance.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Why is my KeyAdapter acting weird? - please don't move to awt/swing

    Are you saying your method is being called two times?
    There are can be several reasons for that:
    How many instances of the class with the method are there?
    How many times has an instance of that class been added as a key listener?

  3. #3
    elliotHenry is offline Member
    Join Date
    Mar 2011
    Posts
    34
    Rep Power
    0

    Default Re: Why is my KeyAdapter acting weird? - please don't move to awt/swing

    Hi Norm

    Answers as follows
    There is only one instance of the class with the method.
    There are 20 times the instance of that class has been added. This is part of my problem(hence the title of the thread). I need to add a key listener to the GUI but don't know where to add it. The only options I have are the buttons or the menu. Everything else is private in the parent class. I think this may be where the issue lies. My menu watcher is fine and my button watcher is fine but the key listener is weird (I can't change the parent class).

    Thanks

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Why is my KeyAdapter acting weird? - please don't move to awt/swing

    Quote Originally Posted by elliotHenry View Post
    Hello

    Sorry about the title, but my last post got moved and since hasn't had any attention
    Please see http://www.java-forums.org/content/113-forum-rules.html

    please don't move to awt/swing
    That's not your choice. There's a reason we have separate sections for disparate topics.

    I'm moving this thread to AWT/Swing and closing it. Any additional post for the same question may lead to a ban. Please continue the discussion in your original thread, in AWT/Swing.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. actionPerformed acting strangely
    By Fortu in forum New To Java
    Replies: 18
    Last Post: 04-10-2011, 05:07 AM
  2. Replies: 6
    Last Post: 01-14-2011, 05:08 PM
  3. Problem using KeyAdapter to get keyboard input
    By DerekRaimann in forum New To Java
    Replies: 0
    Last Post: 12-20-2010, 05:10 AM
  4. Acting Java as if it's C problem
    By reis3k in forum New To Java
    Replies: 13
    Last Post: 10-18-2010, 10:10 AM
  5. KeyAdapter won't fire
    By billq in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 03:37 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
  •