-2

I am trying to create a program where a image is on the screen and if the variable keycode is equal to the key VK_ESCAPE it will remove the image from the screen using the method validate. How can I focus the keylistener so I can run the if statement and validate the image. I have the correct libraries imported and I am getting no errors?!

public void keyPressed(KeyEvent e){
    int keycode = e.getKeyCode();
    if(keycode == KeyEvent.VK_ESCAPE){
     scroll = new ImageIcon("").getImage();
     validate();
     e.consume();
    }
}
loltroll
  • 11
  • 4
  • Is `keyPressed` called at all? The component on which you want to fire the event, must have the keyboard focus. A recommended alternative is to use [key bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) instead of `KeyListener`. Then the component doesn't need to be the focus owner. – Lone nebula Jun 24 '13 at 23:13
  • You need to remove the image from the parent e.g. JPanel and then call revalidate() – My-Name-Is Jun 24 '13 at 23:13

1 Answers1

3

You should use Key Bindings instead of Key Listeners cause with key listeners main issue is that you have to have focus and besides in keybinding you only bind an action to a key, in keylisteners you bind to all. By the way you are not removing anything in your component you have to remove image and then call revalidate().

Tutorial How to use key bindings

Example :

AbstractAction escapeAction = new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent e) {
         myComponent.remove(img); // if you are not using label,if yes label.setImage(null); 
         revalidate();  // im not pretty sure about this 2 lines
         repaint(); //suggested by madProgrammer
    }};
 String key = "ESCAPE";
 KeyStroke keyStroke = KeyStroke.getKeyStroke(key);
 component.getInputMap().put(keyStroke, key);
 component.getActionMap().put(key, escapeAction);
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • While I certainly agree, and explantion as to why the OP should use key bindings of `KeyListener` wouldn't go astray, nothing like blind advice ;) – MadProgrammer Jun 24 '13 at 23:29
  • @MadProgrammer is it correct removing img like this? supposing he has img as final variable – nachokk Jun 24 '13 at 23:30
  • I depends on what the OP is trying to do. Setting the label's icon to `null` might work just as well. You'd still need to revalidate (and possibly repaint) the parent container to get it to update – MadProgrammer Jun 24 '13 at 23:31
  • @MadProgrammer revalidate doesn't call repaint? i edited :) – nachokk Jun 24 '13 at 23:35
  • *"revalidate doesn't call repaint?"* For, that's a difficult question to answer, in my experience, I would say sometimes. Adding `repaint` guarantees that a `repaint` will occur and as the `RepaintManager` consolidates repeated requests, it won't hurt – MadProgrammer Jun 24 '13 at 23:42
  • @MadProgrammer okok i added – nachokk Jun 24 '13 at 23:56
  • What if i wanted to use a key listener and no key binds? please show my code :) Thanks. – loltroll Jun 25 '13 at 00:01
  • @loltroll component.addKeyListener(SameActionAsIPosted); but you have to check if what key is, and may don't work as expected, you have to have focus , also wrapper in `SwingUtilities.invokeLater()`; – nachokk Jun 25 '13 at 00:03
  • @loltroll `KeyListener` suffers from focus issues, meaning the component that they are attached to must be focusable and have focus, which probably part of the problem you are having. It is recommended that you use the key bindings API which over comes this limitation – MadProgrammer Jun 25 '13 at 00:05
  • I am a noob at java graphics programming how would I focus the key listener that I have listed thx :) – loltroll Jun 25 '13 at 00:25
  • @loltroll Programatically with component.requestFocusInWindow() but you are not listening that is discouraged doing that.. – nachokk Jun 25 '13 at 00:31
  • Could you give me direct code so I can copy and paste? My class name is Main. Thank you So Much! :) – loltroll Jun 25 '13 at 00:43
  • @loltroll in SO don't do homework for others you have to effort yourself , i said where i think you are mistaken or if you have problem with something, i won't coding.. intent something if you have some trouble, edit your post and ask again, not asking to copy paste, that's not the way – nachokk Jun 25 '13 at 01:02