0

I want to implement key listener on my puzzle game. i have done it with action listener but now want to move on with key listener.

My logic for action listener was that:

when a specific button is clicked it checks if is adjacent button's icon is null if it is null then their icons will be swapped Now, how can I do it with key listener? Thank you.

if( b1==e.getSource()){

    if(b2.getIcon()==null){
        b2.setIcon(b1.getIcon());
        b1.setIcon(null);
    }
    else if(b5.getIcon()==null){
        b5.setIcon(b1.getIcon());

        b1.setIcon(null);   
    }
 }
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
cute programmer
  • 27
  • 1
  • 11
  • 1
    [How to write a `KeyListener`](https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html) on the Oracle website. – Peter Neyens Jun 18 '15 at 12:14
  • @Constant: please delete your comment above as it's inaccurate. This has nothing to do with his problem, and `==` is just fine here since he is actually after reference equality. – Hovercraft Full Of Eels Jun 18 '15 at 12:17
  • 1
    @ cute programmer, if you need help implementing key press responses in your program, then show us your attempt to do just this, preferably a [Minimal, Complete, and Verifiable Example Program](http://stackoverflow.com/help/mcve). Otherwise you're essentially asking us to create a new tutorial for you when many are already in existence. You're probably much better off using key bindings as answers to many similar questions on this site will tell you. – Hovercraft Full Of Eels Jun 18 '15 at 12:19
  • 1
    [Possible MadProgrammer duplicates](http://stackoverflow.com/search?tab=votes&q=user%3a992484%20%5bkeylistener%5d), and [possible my duplicates](http://stackoverflow.com/search?tab=votes&q=user%3a522444%20%5bkeylistener%5d). – Hovercraft Full Of Eels Jun 18 '15 at 12:50
  • i know how to work with keylistener but in case of many buttons in a single frame, as there are every tile of puzzle a button, and when i press arrow keys on keyboard nothing happanes. – cute programmer Jun 18 '15 at 17:27
  • So in short, you're asking us to help you figure out why your program is not behaving properly, but how can we guess without seeing and testing your code? Again, strongly consider creating and posting an [mcve](http://stackoverflow.com/help/mcve), and again, most similar questions on this and other sites will tell you **not** to use a KeyListener, to go with Key Bindings. Ignore this recommendation at your peril. – Hovercraft Full Of Eels Jun 18 '15 at 17:44

1 Answers1

1

You tell us that you have implemented a KeyListener but it's not working. Without code, all we can do is guess, so here's mine:

  • KeyListeners require focus to work, and so if your GUI has any components that steal the focus, such as JButtons, all JTextComponents such as JTextArea or JTextField, JComboBoxes, JLists,... then your KeyListener won't work.
  • One kludge is to force the component with the KeyListener to have the focus and to greedily hold on to the focus. This is not advisable since it will force your program to behave abnormally since most users expect buttons to have and retain focus when pressed, and text components won't work if they're not allowed focus.
  • Again, often the best solution is to use Key Bindings since these can work without requiring focus and are a cleaner way to capture keystrokes. Please look at the Key Bindings Tutorial and then have a look at my example code for using Key Bindings here and here.

Again for better and more specific help, then please tell us more of the details and show us your pertinent code, preferably as a minimal example program or MCVE.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373