Using Keybinding to get keycode

前端 未结 2 1239
耶瑟儿~
耶瑟儿~ 2021-01-25 08:48

I have a function that display the buffered image. I want mouse listener and key listener to be implemented in JLabel which loads the image. Since JLabel cannot get focus i trie

2条回答
  •  天命终不由人
    2021-01-25 09:11

    I don't know from where you knew JLabel can get Focus. 
    Documentation here clearly explains it cannot get Keyboard Focus.
    

    let's back to the KeyBindings

    myLabel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
        KeyStroke.getKeyStroke((KeyEvent.DEL), 0, false), "DELETE");
    
       myLabel.getActionMap().put("DELETE", new AbstractAction() {
          public void actionPerformed(ActionEvent e) {
              //       any/some Action
          }
       });
    

提交回复
热议问题