Focus problem using key event
Hi!
There is an application I've created uses key event that needs your help.
As you know, that setting 'Mnemonic' to a JButton object makes the button accessible by a key mentioned in the parameter as the following ->
OkButton.setMnemonic(KeyEvent.VK_O);
Now, pressing 'Alt' and 'O' keys together will do the same action as the 'OKButton' does.
But as of me, I think pressing two keys together is not a complete handy job.
So, is there any code that will do the same, by pressing only the 'O' key ?
Ok! I know that there is something to be taken care of; that is, if I want the button to react by pressing only the 'O' key the button must be in focus [value returned by the method isFocusable()
for the button must return true.]
Then how the 'Mnemonic' works ?!! When 'Mnemonic' do something, button does not have any focus.
Only, I press the Alt+O and the work done successfully! No need to take care wherever the focus is. So, is there any way to do alike, where I don't have to manage the focus subsystem? I would only press the 'O' key and the task will be done.
Please send a sample code. Thanks!
[1199 byte] By [
Sunmembera] at [2007-10-1 14:10:03]

I suggest you look into Key Bindings:
"How to Use Key Bindings"
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
Here is a short demo program that uses Key Bindings to do what you describe:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class PressOTest extends JFrame {
public PressOTest() {
super("Press O or C");
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Action that will be associated with the OK button and with
// the 'O' key event
Action okAction = new AbstractAction("Ok") {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(PressOTest.this, "Ok!");
}
};
// Action that will be associated with the Cancel button and with
// the 'C' key event
Action cancelAction = new AbstractAction("Cancel") {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(PressOTest.this, "Cancel!");
}
};
// Register Key Bindings for the 'O' and 'C' keys:
InputMap im = getRootPane().getInputMap(
JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getRootPane().getActionMap();
im.put(KeyStroke.getKeyStroke( KeyEvent.VK_O, 0 ), "ok");
am.put( "ok", okAction );
im.put(KeyStroke.getKeyStroke( KeyEvent.VK_C, 0 ), "cancel");
am.put( "cancel", cancelAction );
// Create and add OK & Cancel buttons:
JButton okButton = new JButton(okAction);
JButton cancelButton = new JButton(cancelAction);
Box box = Box.createHorizontalBox();
box.add( Box.createHorizontalGlue() );
box.add( okButton );
box.add( Box.createHorizontalStrut(10) );
box.add( cancelButton );
box.add( Box.createHorizontalGlue() );
getContentPane().add( box );
setSize(300, 300);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
new PressOTest().setVisible(true);
}
}
So many thanks! Oh! Torgil! You are so good and your code also. You've done what the actually
I need. Your code helped me to solve the entire problem related to the same.
Something I've noticed about you that, you are not only a good programmer, but also a good soul
too. I saw somebody here to write for a reply. But they really don't help the author in trouble.
Only the things they do is speaking meaninglessly and just sending some link to get the help yourself.
They want to avoid & they want to answer!!
But, you are definitely not of them. So, my friend! [if you let me think you as a friend ] you are
awarded with '10 duke dollars'. Excellent job!
You are welcome. Glad I could help :-)
Hi ! Torgil, I'm again!
With you help I've made a code. But still something wrong in my code.
This is the sample code..
I mentioned the problem at the last of this page.
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
<importing All other packages>
public class KeyBindingTest
{
JButton button;
Action LeftAction;
ImageIcon LeftIcon=new ImageIcon(getClass().getResource("/Images/Left.gif"));
public KeyBindingTest() // The constructor
{
<Statements>...................
...............................
createLeftAction();
...............................
button=new JButton(LeftAction);
...............................
}
//CREATING THE LeftAction
private void createLeftAction()
{
LeftAction = new AbstractAction("Left",LeftIcon)
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(this,"Button activated !,"Hey !",JOptionPane.INFORMATION_MESSAGE);
}
};
InputMap im = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getRootPane().getActionMap();
im.put(KeyStroke.getKeyStroke( KeyEvent.VK_LEFT,Event.CTRL_MASK ), "LeftAction");
am.put( "LeftAction", LeftAction );
}
}
The problem is whenever I'm using the following>
im.put(KeyStroke.getKeyStroke( KeyEvent.VK_LEFT,Event.CTRL_MASK ), "LeftAction");
pressing Ctrl + Left arrow results nothing; though clicking on the button still works perfectly !!
But, if I use the following>
im.put(KeyStroke.getKeyStroke( KeyEvent.VK_UP,Event.CTRL_MASK ), "LeftAction");
it works !!!
Then why the previous piece of code does nothing ?!!
Please show me the way to get rid of this.
Please send me something that would also work by pressing the Ctrl + Left arrow key.
Have a nice day!
My Operating System: Microsoft Windows XP
I'm using j2sdk1.4.1_01
