Problem with accelerator keys while JMenuBar is invisible

Hello friends,

I am calling setVisible(false) method of JMenuBar which I have already added in JFrame. It is getting invisible properly. But now the problem is that whatever accelerator keys I have set for the JMenuItem in the menubar, are not getting fired.

What can be reason for this?

Please help me out to solve this problem with ur expert comments...

Thanx in advance

[403 byte] By [heet_patel] at [2007-9-27 16:46:21]
# 1
Do the accelerator keys work properly when the JMenuBar is visible? Do you get any exceptions?
afotoglidis at 2007-7-6 1:04:22 > top of Java-index,Archived Forums,Swing...
# 2
yeah.....accelerator keys are very well working when JMenuBar is visible but after making it invisible accelerators are not working...Do you know any solution for this....
heet_patel at 2007-7-6 1:04:22 > top of Java-index,Archived Forums,Swing...
# 3

Sounds like normal behaviour to me. If you can't see the menu item or it is disabled, then you shouldn't be able to use its accelerator key.

Here is a way to map keystrokes to an action without using menu items. In this case Ctrl+C is handled by the text area when it has focus.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.text.*;

import javax.swing.text.DefaultEditorKit;

public class TestKeyboardAction extends JFrame

{

JTextArea textArea;

public TestKeyboardAction()

{

JPanel panel = new JPanel()

{

public boolean isFocusTraversable()

{

return true;

}

};

setContentPane( panel );

textArea = new JTextArea( "one two three four", 5, 30 );

panel.add(textArea);

JMenuBar menuBar = new JMenuBar();

setJMenuBar( menuBar );

JMenu menu = new JMenu( "Actions" );

menuBar.add( menu );

JMenuItem menuItem;

// create a menu item using the Display Word Action

Action displayWordAction = new DisplayWordAction();

menuItem = new JMenuItem( displayWordAction );

menuItem.setAccelerator( (KeyStroke)displayWordAction.getValue(Action.ACCELERATOR_KEY) );

menu.add( menuItem );

// create a keyboard binding for the Display Character Action

Action displayCharacterAction = new DisplayCharacterAction();

Object key = displayCharacterAction.getValue(Action.NAME);

KeyStroke ks = (KeyStroke)displayCharacterAction.getValue(Action.ACCELERATOR_KEY);

InputMap im = textArea.getInputMap();

//InputMap im = panel.getInputMap(JComponent.WHEN_FOCUSED);

//InputMap im = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

im.put(ks, key);

ActionMap am = textArea.getActionMap();

//ActionMap am = panel.getActionMap();

am.put(key, displayCharacterAction);

}

public static void main(String[] args)

{

TestKeyboardAction frame = new TestKeyboardAction();

frame.setDefaultCloseOperation( EXIT_ON_CLOSE );

frame.pack();

frame.setVisible(true);

}

class DisplayWordAction extends AbstractAction

{

public DisplayWordAction()

{

putValue( Action.NAME, "Display Word" );

putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_W, KeyEvent.CTRL_MASK) );

}

public void actionPerformed(ActionEvent e)

{

System.out.println( "Display Word" );

}

}

class DisplayCharacterAction extends AbstractAction

{

public DisplayCharacterAction()

{

putValue( Action.NAME, "Display Character" );

putValue( Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_MASK) );

}

public void actionPerformed(ActionEvent e)

{

System.out.println("Display Character");

}

}

}

camickr at 2007-7-6 1:04:22 > top of Java-index,Archived Forums,Swing...