Help with KeyListener

I'm learning how KeyListener works, but I couldn't figure out why the following code isn't responding to keystrokes. Please help.

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class KeyTest extends Applet implements KeyListener

{

Label lblOutput = new Label("No Key Pressed");

public void init()

{

setLayout(new BorderLayout());

add(lblOutput, BorderLayout.CENTER);

addKeyListener(this);

this.requestFocus();

}

public void keyPressed(KeyEvent e) {

}

public void keyReleased(KeyEvent e) {

}

public void keyTyped(KeyEvent e) {

lblOutput.setText("Key Pressed!");

}

}

[728 byte] By [kevch27a] at [2007-11-27 4:52:50]
# 1

1) It does respond, but it needs focus first. If you click on the applet window then type, you'll see keystrokes being processed.

2) Please use code tags as they make your code readable. Compare your code w/ this, your code w/ tags:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

public class MyKeyListener extends Applet implements KeyListener

{

JLabel lblOutput = new JLabel("No Key Pressed");

public void init()

{

setLayout(new BorderLayout());

add(lblOutput, BorderLayout.CENTER);

addKeyListener(this);

this.requestFocus();

}

public void keyPressed(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public void keyTyped(KeyEvent e)

{

lblOutput.setText("Key Pressed!");

System.out.println("in keyTyped -- Key Pressed!");

}

}

Easier to read?

petes1234a at 2007-7-12 10:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

I ran into more problems. When I try to add a new panel to Borderlayout, the KeyListener stopped working again. This drives me nuts, please see below and help me.

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.applet.*;

public class MyKeyListener extends Applet implements KeyListener

{

JLabel lblOutput = new JLabel("No Key Pressed");

Panel aPanel = new Panel();

public void init()

{

setLayout(new BorderLayout());

add(lblOutput, BorderLayout.CENTER);

add(bottomPad, BorderLayout.SOUTH);

//Why above line stops KeyListener?

addKeyListener(this);

this.requestFocus();

}

public void keyPressed(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

public void keyTyped(KeyEvent e)

{

lblOutput.setText("Key Pressed!");

System.out.println("in keyTyped -- Key Pressed!");

}

}

kevch27a at 2007-7-12 10:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 3

Have have been asked to use the "Code" tags when posting code.

KeyEvents only go to the component that has focus. By default a panel isn't focusable so it will never receive any key events.

I don't know what you are trying to do the the correct way to handle KeyStrokes is to use [url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]Key Bindings[/url].

camickra at 2007-7-12 10:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 4

I'm trying to create a simple blackjack problem but instead of using bottons for commands I'm using keystrokes for commands suchs as 'hit' and 'stand'.

The problem that I have right now is when adding the BorderLayout.CENTER for the playing area. So far I've tried Canvas and JTable. I understand Canvas can't be focus upon but I thought JTable would've worked, but as soon as I add JTable the keyListener stop responding to keystrokes.

Is there another way around it instead of going into Key Bindings? I mainly just want to figure out the KeyListener, keyPressed, keyReleased, and keyTyped for this project.

kevch27a at 2007-7-12 10:06:59 > top of Java-index,Desktop,Core GUI APIs...
# 5
I told you that only the component that has focus receives the KeyStrokes. So when you add a table to the frame it now has focus so the KeyListener needs to be added to the table.
camickra at 2007-7-12 10:06:59 > top of Java-index,Desktop,Core GUI APIs...