JButton b = new JButton("OK"); b.setMnemonic(KeyEvent.VK_ENTER); //Bouton Enter JButton p = new JButton("Cancel"); p.setMnemonic(KeyEvent.VK_ESCAPE); //Bouton Echap
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprotected JRootPane createRootPane() { ActionListener actionListenerForCancel= new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { dispose(); } }; ActionListener actionListenerForOK= new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { valider(); } }; KeyStroke strokeCancel= KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); KeyStroke strokeOK= KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); JRootPane rootPane= new JRootPane(); rootPane.registerKeyboardAction( actionListenerForCancel, strokeCancel, JComponent.WHEN_IN_FOCUSED_WINDOW); rootPane.registerKeyboardAction( actionListenerForOK, strokeOK, JComponent.WHEN_IN_FOCUSED_WINDOW); return rootPane; }