Trying to add a key keylistener to a program i am making but it wont work. cant find the reason :/ any help would be awesome:)

Display.class

package com.mono.main;

import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.mono.main.handler.Controller;
import com.mono.main.handler.Game;
import com.mono.main.handler.InputHandler;

public class Display extends JPanel implements Runnable {
	
	private static final long serialVersionUID = 1L;
	
	public static final int 		WIDTH = 800;
	public static final int 		HEIGHT = 600;
	public static final String 		TITLE = "Minefront Pre-Alpha 0.01";
	
	private boolean 				running = false;
	
	private Thread 					thread;
	private InputHandler			input;
	private Dude					d;
	private Game 					gamer;
	
	public Display(){
		d = new Dude();
		gamer = new Game();
		input = new InputHandler();
		addKeyListener(input);
		System.out.println("HEllo");
	}
	
	
	public void run() {
		while(running){
				tick();
				repaint();
		}
	}
	private void tick(){
		gamer.tick(input.key);
	}
	
	public void paint(Graphics g){
		super.paint(g);
			Graphics2D g2d = (Graphics2D)g;
			
			d.drawDude(g2d);
	}
	
	
	public static void main(String[] args){
		Display game = new Display();
		JFrame frame = new JFrame();
		
		frame.add(game);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(WIDTH, HEIGHT);
		frame.setResizable(false);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		game.start();
	}


	private void start() {
		if(running)
			return;
		running = true;
		thread = new Thread(this);
		thread.start();
		
	}
	
	
}

InputHandler.class

package com.mono.main.handler;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class InputHandler implements KeyListener {
	
	public boolean[] key = new boolean[68836];
	
	public InputHandler(){
		System.out.println("Input Conctructur");
	}
	
	
	@Override
	public void keyPressed(KeyEvent e) {
		System.out.println("Key pressed");
		int keyCode = e.getKeyCode();
		if(keyCode > 0 && keyCode < key.length){
			key[keyCode] = true;
		}
			 
	}
	@Override
	public void keyReleased(KeyEvent e) {
		int keyCode = e.getKeyCode();
		if(keyCode > 0 && keyCode < key.length){
			key[keyCode] = false;
		}
		
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}

}

Game.class

package com.mono.main.handler;

import java.awt.event.KeyEvent;



public class Game {
	
	public Controller control;
	
	public Game(){
		control = new Controller();
	}

	public void tick(boolean[] key){
		
		//System.out.println(key);
		boolean left = key[KeyEvent.VK_A];
		boolean right = key[KeyEvent.VK_D];
		boolean jump = key[KeyEvent.VK_SPACE];
		
		control.tick(left, right, jump);
	}
	
	
}

Controller.class

package com.mono.main.handler;


public class Controller {
	
	public int x, y;
	
	public void tick(boolean left, boolean right, boolean jump){
		//System.out.println();
		if(left){
			System.out.println("Game Left");
			x--;
		}
		if(right){
			x++;
		}
		if(jump){
			
		}
	}
}

Dude.class

package com.mono.main;

import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;

import com.mono.main.handler.Controller;

public class Dude {
	
	Image 					dudeImage;
	Controller 				c;
	
	public Dude(){
		ImageIcon i = new ImageIcon("Dude.gif");
		dudeImage = i.getImage();
		c = new Controller();
		
	}
	
	public void drawDude(Graphics2D g2d){
		g2d.drawImage(dudeImage,c.x,c.y,null);
	}
}

it wont work.

Please explain. Do you get errors? Please post the full text of the error message.
Do the listener methods get called?
Does the component with the listeners have the focus?
Can the component with the listeners receive the focus? Not all components do this automatically, you need to enable them.

I don't get any errors. when i run the program and hit a key it just doesn't react
the component with the listener has focus.
I think the component with the listener is focusable.

I think the component with the listener is focusable.

Add a focus listener to see if the component does get the focus.

it seems like it doesn't get focus.
when i try to put it all in to one file i get an error saying it should be in separate file

There are methods you can call that will request that a component be capable of getting the focus and to get the focus. Try some of those.

Swing JComponets are designated to use KeyBinding rather than KeyListener,

1) you can ignore Focus and Focus SubSystem (both came from Native OS and is asynchrounous)

2) you can override system reserved Keys

3) more code easilly and confortable that catching Focus somewhere in the Screen

4) for KeyListener you have to setFucusable()

Changed public class Display extends JPanel to public class Display extends Canvas.
the key listener works now:D thanks to every one who replied

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.