setAccelerator for jmenu item in java

365 views Asked by At

I'm learning java with Swing. I have a problem with setAccelerator for menu. My code:

    closeItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_W, ActionEvent.CTRL_MASK));
    closeOtherItem.setAccelerator(KeyStroke.getKeyStroke(
    KeyEvent.VK_W, ActionEvent.CTRL_MASK&ActionEvent.SHIFT_MASK));

I want when I press Ctrl+W, my JTabbedPane will close selectedTab, and when press Ctrl+Shift+W my JtabbedPane will close all ignore selectedTab. But only action Ctrl+W was detected, although when I click on closeOtherItem my program will run normally.

1

There are 1 answers

0
MadProgrammer On

Try or'ing the modifers instead of and'ing and use InputEvent.CTRL_DOWN_MASK and InputEvent.SHIFT_DOWN_MASK instead...

KeyStroke.getKeyStroke(KeyEvent.VK_W, 
                       InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK)