Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

MouseListener working but not KeyListener

807606May 5 2007 — edited May 5 2007
hey guys, im new here its my first post
can you check out this code and tell why the key listener isn't working even though its implemented in exactly the same way that the mouse listener is
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JPanel;


public class KeyListenerTest extends JPanel {
	
	

	public KeyListenerTest(){
		addKeyListener(new KeyListenerTestKeyListener());
		addMouseListener(new KeyListenerTestMouseListener());
		
		setBackground(Color.WHITE);
		setPreferredSize(new Dimension(400,400));
	}

	public class KeyListenerTestKeyListener implements KeyListener {

		public void keyPressed(KeyEvent arg0) {
			System.out.println("key pressed");

		}

		public void keyReleased(KeyEvent arg0) {}

		public void keyTyped(KeyEvent arg0) {}

	}
	
	public class KeyListenerTestMouseListener implements MouseListener {

		public void mouseClicked(MouseEvent arg0) {
			System.out.println("Mouse clicked");
		}

		public void mouseEntered(MouseEvent arg0) {}

		public void mouseExited(MouseEvent arg0) {}

		public void mousePressed(MouseEvent arg0) {}

		public void mouseReleased(MouseEvent arg0) {}

	}
	
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2007
Added on May 5 2007
2 comments
591 views