Re: KeyListener not working




Hello, probably you forgot about set component focus

requestFocusInWindow
--------------------
public boolean requestFocusInWindow()Requests that this Component gets the input focus. Refer to Component.requestFocusInWindow() for a complete description of this method.
If you would like more information on focus, see How to Use the Focus Subsystem, a section in The Java Tutorial.


Justin wrote:
I'm just messing around, getting ready to write a game for fun and
can't get this KeyListener to work. I am really not sure why. The
code below should run without modification. Thanks.

package javaapplication1;

import javax.swing.JPanel;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class Board extends JPanel implements KeyListener{

private final int BOARD_WIDTH = 12;
private final int BOARD_HEIGHT = 20;
private final int BLOCK_SIZE = 10;
//private Piece pieceBoard[][] = new Piece[BOARD_WIDTH]
[BOARD_HEIGHT];

public Board() {
setPreferredSize(new java.awt.Dimension(BOARD_WIDTH *
BLOCK_SIZE, BOARD_HEIGHT * BLOCK_SIZE));
setSize(getPreferredSize());
setBackground(java.awt.Color.WHITE);
setLayout(null);

addKeyListener(this);
addKeyListener(new KeyListener(){

public void keyTyped(KeyEvent e) {
System.out.println("key event");
}

public void keyPressed(KeyEvent e) {
System.out.println("key event");
}

public void keyReleased(KeyEvent e) {
System.out.println("key event");
}

});

System.out.println(getKeyListeners());
}

public static void main (String args[]){
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setBounds(10, 10, 200, 300);
java.awt.Container c = frame.getContentPane();
c.add(new Board());
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public void keyTyped(KeyEvent e) {
System.out.println("key event");
}

public void keyPressed(KeyEvent e) {
System.out.println("key event");
}

public void keyReleased(KeyEvent e) {
System.out.println("key event");
}
}
.



Relevant Pages

  • Re: Sortable Java Tree Table
    ... I have never actually done this, but if I wanted to sort a JTree, ... private Listener listener = new Listener; ... public Object getChild(Object parent, int index) { ... public void valueForPathChanged{ ...
    (comp.lang.java.gui)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • Re: NotSerializable object issue...
    ... private static final NoopOutputStream DUMMY_OUTPUT_STREAM = new ... public SerializableCheckerthrows IOException { ... public void writeObjectthrows IOException { ... private int counter; ...
    (comp.lang.java.programmer)
  • Re: Im needing help with compile errors GameOfWar new compile errors under 2judith
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)
  • Re: Im needing help with compile errors for GUI enviroment GameOfWar
    ... public static int[] randomize ... private Rank ... public void paintComponent ... class WinningsPanel extends JPanel implements ItemListener ...
    (comp.lang.java.gui)