1

I try to make an JMenuBar hide and show with the ctrl+h keystroke, i've succeed to do this for hidding the JMenu but I can't use ctrl+h to show the JMenu, here he's the code :

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;    
import view.Menu;
import view.Window;

public class MenuController implements ActionListener {

    protected Window w;
    protected Menu m;

    public MenuController(Window w) {
        this.w = w;
        this.m = w.getMenu();
        m.getQuit().addActionListener(this);
        m.getHide().addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String item = e.getActionCommand();
        if (item.equals("Quitter"))
            System.exit(0);
        if(item.equals("Masquer le menu")) {
            if(m.isVisible() == true)
                m.setVisible(false);
            else
                m.setVisible(true);
        }
    }    
}

getHide(), give the JMenuItem who have the keystroke, if you see where he's the problem... Regards

Edit : So here, the view.Menu class :

public class Menu extends JMenuBar {

private static final long serialVersionUID = 1L;
private JMenuItem quit = new JMenuItem();
private JMenuItem hide = new JMenuItem();


public Menu() {
    JMenu menu1 = new JMenu("Fichier");

    hide.setText("Masquer le menu");
    hide.setEnabled(true);
    hide.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, KeyEvent.CTRL_MASK));
    menu1.add(hide);

    quit.setText("Quitter");
    quit.setEnabled(true);
    quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK));
    menu1.add(quit);

    add(menu1);
}

public JMenuItem getQuit() {
    return quit;
}

public JMenuItem getHide() {
    return hide;
}

}

zed13
  • 357
  • 4
  • 21
  • What is the superclass of `view.Menu`? What currently happens when you activate the "Hide" menu item? – VGR Oct 12 '13 at 13:29
  • So when i active the hide menu item, the menu is not currently displayed, my problem is when i want to show the menu again, the keystroke doesn't work anymore. – zed13 Oct 12 '13 at 13:36
  • 3
    accelerators aren't working if the menuBar isn't showing, see a [recent QA for details](http://stackoverflow.com/a/18097498/203657) – kleopatra Oct 12 '13 at 14:46

1 Answers1

1

Like kleopatra mentioned in the comments, your accelerator won't work if the menu is hidden. If you don't need that key combination for anything else, one way around this problem (a little less dirty, I think, than the tricks mentioned in the link kleopatra posted) would be to register a key binding on the component containing the menu bar that performs the same set of actions.

Josh
  • 1,563
  • 11
  • 16
  • when it comes to hacks, there's rarely any general order of preference :-) Duplicating the binding is a good way out if there aren't too many items – kleopatra Oct 13 '13 at 09:31
  • Thanks guys for the answer, I decide to use the key binding, again thanks :) – zed13 Oct 13 '13 at 09:43
  • @kleopatra Definitely :). I didn't mean to disparage any of the tricks you mentioned (actually, I was reading on my phone and didn't notice you had written the other answer); I've just used key bindings to similar effect in the past, so that's what stuck out to me first. – Josh Oct 13 '13 at 19:45