user2005938
user2005938

Reputation: 179

KeyListener not working

public class KL implements KeyListener {

 public static void main(String[] args) {
        final JPopupMenu popup = new JPopupMenu();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.setVisible(true);

    }

@Override
public void keyPressed(KeyEvent arg0) {
    System.out.println(arg0.getKeyChar());
}

@Override
public void keyReleased(KeyEvent e) {
    System.out.println(e.getKeyChar());

}

@Override
public void keyTyped(KeyEvent e) {
    System.out.println(e.getKeyChar());

}
}

That's my class, it's probably something really stupid on my part, but my KeyListener here is not working. Nothing comes up on the console.

Upvotes: 0

Views: 204

Answers (2)

fletch367
fletch367

Reputation: 53

I know this is an old post but I wanted to put this online so someone like myself can find it....

I worked on this problem for hours before figuring it out. Make sure that your Component has focus. For example I have all of my activity going on in a custom JPanel named SpaceShipPanel:

class SpaceShipPanel
{
    //instance variables
    //Now my constructor
    SpaceShipPanel(){
        //bla bla blah
        setFocusable(true);//THIS LINE IS WHAT SAVED ME!!
    }
}

From what I hear, keyBindings are the best route but the class I'm taking didn't cover this topic. Hopefully this will save someone hours of beating their heads against the wall.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347332

Let's start with the fact that you're not attached the listener to anything, then move on to the fact that you really should be using Key Bindings

And with example

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestTableEditing {

    public static void main(String[] args) {
        new TestTableEditing();
    }

    public TestTableEditing() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel key;
        private int counter = 0;

        public TestPane() {
            key = new JLabel("...");
            add(key);
            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = getActionMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A.pressed");
            am.put("A.pressed", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("A was pressed");
                    key.setText("A was pressed " + (++counter));
                }
            });
        }

    }

}

Upvotes: 2

Related Questions