Re: KeyListeners: Automatic Focus?
Quote:
Originally Posted by
Norm
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):
Code :
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.
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.
Re: KeyListeners: Automatic Focus?
Quote:
Originally Posted by
Norm
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.
Re: KeyListeners: Automatic Focus?
Give HashMaps a key and they return a value associated with that key.
Code :
...
} else if(key.equals("3")) {
JButton b = mainButtonList.get(8);
...
vs
Code :
JButton b = theHashMap.get(key); // get the Button associated with the key
You load the HashMap by associating the button with the key.
Re: KeyListeners: Automatic Focus?
Quote:
Originally Posted by
Norm
Give HashMaps a key and they return a value associated with that key.
Code :
...
} else if(key.equals("3")) {
JButton b = mainButtonList.get(8);
...
vs
Code :
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.
Re: KeyListeners: Automatic Focus?
Quote:
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:
Code :
addKeyAccelerator(buttonPanel, dbc, "action2", KeyEvent.VK_ENTER, 0);
Re: KeyListeners: Automatic Focus?
Quote:
Originally Posted by
Norm
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:
Code :
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.
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.