Results 1 to 1 of 1
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Rep Power

    Default Problem using KeyAdapter to get keyboard input

    *** IGNORE - I accidentally typed "public void keyType, instead of public void keyTyped", my mistake!!! It works now and the code is revised.

    I am working through a book on Java 6, and I got to an example that makes an application that takes user input and displays it in a frame. However, the program does not work and the only way I could make it work was to implement the KeyListener class to my main class and implement the keyPressed, keyReleased, and keyTyped methods. I got it to work, but I want to be able to take advantage of using adapters, so I would really appreciate if someone could show me how to make this code work properly.

    Java Code:
    	import java.awt.*;
    	import java.awt.event.*;
    	import javax.swing.*;
    	public class KeyChecker extends JFrame {
    		JLabel keyLabel = new JLabel("Hit any key");
    		public KeyChecker() {
    			super("Hit a Key");
    			setSize(300, 200);
    			setLayout(new FlowLayout(FlowLayout.CENTER));
    			KeyMonitor monitor = new KeyMonitor(this);
    		public static void main(String[] arguments) {
    			new KeyChecker();
    	class KeyMonitor extends KeyAdapter {
    		KeyChecker display;
    		KeyMonitor(KeyChecker display) {
    			this.display = display;	
    		public void keyTyped(KeyEvent event) {
    			display.keyLabel.setText("" + event.getKeyChar());
    Thanks in advance for any help you can give me with this!

    -Derek Raimann
    Last edited by DerekRaimann; 12-20-2010 at 04:37 AM.

Similar Threads

  1. Read input from keyboard
    By bison in forum New To Java
    Replies: 2
    Last Post: 11-20-2010, 06:48 PM
  2. Keyboard Input In A Seperate Class?
    By FatalSylence in forum New To Java
    Replies: 6
    Last Post: 10-12-2010, 05:29 AM
  3. How to use another image using a keyboard input
    By Rekuta in forum New To Java
    Replies: 0
    Last Post: 05-13-2010, 05:00 PM
  4. get keyboard input while running in the background?
    By gen1mx6 in forum Advanced Java
    Replies: 16
    Last Post: 07-16-2009, 03:51 PM
  5. Polled keyboard input through swing
    By Prometheus in forum Advanced Java
    Replies: 2
    Last Post: 02-04-2008, 04:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts