0

I'm trying to add a keylistener to my JPanel but it won't work even after this:

put.setFocusable(true);
    put.requestFocusInWindow();
    KeyboardHandler keyhandler=new KeyboardHandler();
    put.addKeyListener(keyhandler);
}
private class KeyboardHandler implements KeyListener{
    public void keyPressed(KeyEvent e) {
    System.out.println("OVDE ZZZ");
    if(e.getKeyCode()==17) ctrl=true;
        if(e.getKeyCode()==90) z=true;
        if(ctrl && z){
            if (UndoBrojac==0) JOptionPane.showMessageDialog(null, "You can't undo that");
            else{
                UndoBrojac--;
                put.setUndo(UndoBrojac);
            }
        }

}

public void keyReleased(KeyEvent e){
    if (e.getKeyCode()==17) ctrl = false;
    if (e.getKeyCode()==90) z = false;
}

    public void keyTyped(KeyEvent e) {}
}

I'm trying to add ctrl+z but it doesn't work.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    You should use `KeyEvent.VK_CONTROL` and `KeyEvent.VK_Z` to avoid magic constants. – Dahaka Jul 11 '13 at 10:39
  • thats not a problem now, it won't even get to keypressed – Nemanja Neca Stojanovic Jul 11 '13 at 10:41
  • 3
    For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. – Andrew Thompson Jul 11 '13 at 10:56
  • 1
    Why would it work ? It requires the component to have focus, and I guess in your terms the top-level container is getting the focus, so use KeyBinding as already suggested :-) This post regarding [Motion using Keyboard](http://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/), by @camickr will definitely interest you :-) – nIcE cOw Jul 11 '13 at 11:06
  • I found that focus must be done after being set to visible, so creating a new class now – Nemanja Neca Stojanovic Jul 11 '13 at 11:09
  • what happened when you typed _swing keylistener not working_ into the search field in the upper trailing edge of this or any other page of the site? – kleopatra Jul 11 '13 at 11:12
  • A lot of stuff appeared, tried a lot of them. Didn't work – Nemanja Neca Stojanovic Jul 11 '13 at 11:13
  • 1
    well, that's probably because you didn't follow the answers ;-) The one I choose as the duplicate (in voting to close) mentions both the focus issue and that you should _not_ use a keyListener - so why didn't you learn from that, update your approach accordingly and then come back? – kleopatra Jul 11 '13 at 11:16
  • See also [*Implementing Undo and Redo*](http://docs.oracle.com/javase/tutorial/uiswing/components/generaltext.html#undo). – trashgod Jul 11 '13 at 13:30

1 Answers1

2

Several helpful comments suggest using Key Bindings. As shown here, it is particularly easy to specify KeyEvent.VK_Z as an ACCELERATOR_KEY in the context of an Action. If you are undoing changes to a text component, see How to Write an Undoable Edit Listener; the TextComponentDemo cited there illustrates a typical UndoAction.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045