Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 2 FirstFirst 12
Results 26 to 33 of 33

Thread: KeyListeners: Automatic Focus?

  1. #26
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: KeyListeners: Automatic Focus?

    Quote Originally Posted by Norm View Post
    Before when?
    Are you looking at my post #22?
    I think I got it working. I put a keyListener on the mainFrame to the class ListenForKeys and method keyTyped(KeyEven ke)

    It looks like this (and yes I know this is repetitive but it was an easy solution):
      class ListenForKeys extends KeyAdapter {
        public void keyTyped(KeyEvent ke) {
          char keyChar = ke.getKeyChar();
          String key = Character.toString(keyChar);
          if(key.equals("0")) {
            JButton b = mainButtonList.get(9);
            b.doClick();
          } else if(key.equals("1")) {
            JButton b = mainButtonList.get(6);
            b.doClick();
          } else if(key.equals("2")) {
            JButton b = mainButtonList.get(7);
            b.doClick();
          } else if(key.equals("3")) {
            JButton b = mainButtonList.get(8);
            b.doClick();
          } else if(key.equals("4")) {
            JButton b = mainButtonList.get(3);
            b.doClick();
          } else if(key.equals("5")) {
            JButton b = mainButtonList.get(4);
            b.doClick();
          } else if(key.equals("6")) {
            JButton b = mainButtonList.get(5);
            b.doClick();
          } else if(key.equals("7")) {
            JButton b = mainButtonList.get(0);
            b.doClick();
          } else if(key.equals("8")) {
            JButton b = mainButtonList.get(1);
            b.doClick();
          } else if(key.equals("9")) {
            JButton b = mainButtonList.get(2);
            b.doClick();
          } else if(key.equals("+")) {
            JButton b = operators.get(0);
            b.doClick();
          } else if(key.equals("-")) {
            JButton b = operators.get(1);
            b.doClick();
          } else if(key.equals("x")||key.equals("*")) {
            JButton b = operators.get(2);
            b.doClick();
          } else if(key.equals("/")) {
            JButton b = operators.get(3);
            b.doClick();
          }
          System.out.println(key);
        }
      }

    One problem though... how do I register a keyStroke for the ENTER key? The VK_ENTER key code doesn't seem to work... it just keeps adding line breaks in the console.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: KeyListeners: Automatic Focus?

    With repeating code like you have, there must be a way to put all that in an array or a HashMap.

  3. #28
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: KeyListeners: Automatic Focus?

    Quote Originally Posted by Norm View Post
    With repeating code like you have, there must be a way to put all that in an array or a HashMap.
    If you have a suggestion please let me know. I haven't learned about HashMaps so I... well honestly have no clue what they are.

    I'm not sure how I would put all those conditional statements in an array. Again, if you have a suggestion, I would love to hear it. I write too much repetitive code. You would be helping me quite a bit.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: KeyListeners: Automatic Focus?

    Give HashMaps a key and they return a value associated with that key.
      ...
    } else if(key.equals("3")) {
            JButton b = mainButtonList.get(8);
     ...
    vs
      JButton b = theHashMap.get(key);  // get the Button associated with the key

    You load the HashMap by associating the button with the key.

  5. #30
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: KeyListeners: Automatic Focus?

    Quote Originally Posted by Norm View Post
    Give HashMaps a key and they return a value associated with that key.
      ...
    } else if(key.equals("3")) {
            JButton b = mainButtonList.get(8);
     ...
    vs
      JButton b = theHashMap.get(key);  // get the Button associated with the key

    You load the HashMap by associating the button with the key.
    What does that replace in this code to make it more efficient?

    And by the way, do you know how to get the program to register a key event for the ENTER key? I can't get it to respond... it just adds line breaks in the console.

    keyCode VK_ENTER doesn't work either. Unless I did it wrong.

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: KeyListeners: Automatic Focus?

    What does that replace in this code to make it more efficient?
    Post #26 has over 26 if/else if and assignment statements.
    You can replace ALL of them with the one statement in post #29
    However you have to load the HashMap so its not that much less code.

    This worked for me:
        addKeyAccelerator(buttonPanel, dbc, "action2", KeyEvent.VK_ENTER, 0);

  7. #32
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: KeyListeners: Automatic Focus?

    Quote Originally Posted by Norm View Post
    Post #26 has over 26 if/else if and assignment statements.
    You can replace ALL of them with the one statement in post #29
    However you have to load the HashMap so its not that much less code.

    This worked for me:
        addKeyAccelerator(buttonPanel, dbc, "action2", KeyEvent.VK_ENTER, 0);
    I'm not very familiar with HashMaps so I guess I will just stick with the current code structure in #26.

    I'm using KeyListeners in the main JFrame. I either need a character or keyCode for the ENTER key but the keyCode didn't appear to work.

    EDIT: Just noticed the API states that the keyCode method call returns VK_UNDEFINED for keyTyped events. There's my problem! I guess I will just make a keyReleased event response for the ENTER key.
    Last edited by bgroenks96; June 24th, 2011 at 07:54 PM.

  8. #33
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: KeyListeners: Automatic Focus?

    I use if/else statements A LOT in Java programming. If you have a better/more advanced way of doing things, PLEASE tell me. You will make my life so much easier.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 0
    Last Post: December 26th, 2010, 10:15 AM
  2. Maintain focus on JFrame
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 15th, 2010, 08:49 AM
  3. [SOLVED] JTextPane focus problem
    By LeonLanford in forum AWT / Java Swing
    Replies: 3
    Last Post: June 21st, 2010, 11:50 PM
  4. switch focus to another window
    By tuansoibk in forum AWT / Java Swing
    Replies: 1
    Last Post: November 13th, 2009, 02:02 PM
  5. Automatic correction of brightness and contrast
    By Mirko in forum Java Theory & Questions
    Replies: 1
    Last Post: September 16th, 2009, 06:26 PM