Results 1 to 4 of 4
  1. #1
    t338327 is offline Member
    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Default Using KeyListener, as opposed to MouseListener

    Hello. I have a program which I wrote as an example below:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class Stuff extends JPanel implements KeyListener, MouseListener
    {
    	private static final long serialVersionUID = 1L;
    	boolean isRunning = true;
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame("Hello");
    		frame.add(new Stuff());
    		frame.setSize(100, 100);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    
    	public Stuff()
    	{
    		addKeyListener(this);
    		addMouseListener(this);
    		Thread thread = new Thread(new Runnable()
    		{
    			public void run()
    			{
    				while(isRunning)
    				{
    					try
    					{
    						Thread.sleep(5);
    					} 
    					catch (InterruptedException e)
    					{
    						e.printStackTrace();
    					}
    				}
    			}
    		});
    	}
    	public void keyPressed(KeyEvent e)
    	{
    		System.out.println("Key Pressed");
    		if(e.getKeyChar() == 27)
    		{
    			isRunning = false;
    			System.exit(1);
    		}
    	}
    	public void keyReleased(KeyEvent e) {}
    	public void keyTyped(KeyEvent e) {}
    	public void mouseClicked(MouseEvent e)
    	{
    		System.out.println("Mouse Clicked");
    	}
    	public void mouseEntered(MouseEvent e) {}
    	public void mouseExited(MouseEvent e) {}
    	public void mousePressed(MouseEvent e) {}
    	public void mouseReleased(MouseEvent e) {}
    }
    Whenever I click in the window that pops up, It prints "Mouse Clicked"
    However, whenever I type anything, including esc (Whose value is 27), nothing is printed, and the program doesn't exit either.
    Why is this? I used MouseListener and KeyListener in the exact same way, so why doesn't KeyListener work?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,563
    Rep Power
    23

    Default Re: Using KeyListener, as opposed to MouseListener

    Make sure the component with the key listener can get the focus and has the focus. The Component class has some methods that will do that. Look at the API doc for Component and Find "focus" to find the methods
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Using KeyListener, as opposed to MouseListener

    Swing is designed to work with Key Bindings, which have three options for the focused state. Offhand, I can't think of a single situation where you would need to use a KeyListener in a Swing application.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Using KeyListener, as opposed to MouseListener

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. MouseListener for everything?
    By DFTBA in forum New To Java
    Replies: 2
    Last Post: 07-22-2012, 01:07 PM
  2. Replies: 3
    Last Post: 01-02-2012, 05:35 AM
  3. Replies: 5
    Last Post: 12-21-2011, 10:45 PM
  4. Replies: 10
    Last Post: 11-27-2009, 10:52 AM
  5. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM

Posting Permissions

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