Results 1 to 6 of 6
  1. #1
    Darwanism is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default keyboard listener not taking effect

    hi guys i was testing few listeners around and i've typed the code bellow which is supposed to catch a keyboard's button when it's clicked and display it , for some reason it doesn't seem to take effect i'm sure i'm doing a dump mistake somewhere but i can't tell where if someone could help with this and thx
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class keyevent extends JFrame{
    	keyeventPanel keyevent1= new keyeventPanel();
    		public keyvent(){			
    			add(keyevent1);
    			keyevent1.setFocusable(true);			
    		}
    		
    	
    	public static void main(String[] args) {
    		keyevent frame= new keyevent();
    		frame.setTitle("keyboard");
    		frame.setSize(200, 200);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    	
    	
    	static class keyeventPanel extends JPanel{
    		private int x=100;
    		private int y=100;
    		private char key='A';
    		public keyeventPanel(){
    			addKeyListener(new KeyAdapter(){
    				public void KeyPressed(KeyEvent e){
    					switch(e.getKeyCode()){
    					
    						case KeyEvent.VK_DOWN:	y+=10;	break;
    						case KeyEvent.VK_UP:	y-=10;	break;
    						case KeyEvent.VK_LEFT:	x-=10;	break;
    						case KeyEvent.VK_RIGHT:	x+=10;	break;
    						default: key = e.getKeyChar();
    					}
    					repaint();
    				}				
    			});			
    		}
    		
    		protected void paintComponent(Graphics e){
    			super.paintComponent(e);
    			e.setFont(new Font("TimesRoman", Font.PLAIN, 24));
    			e.drawString(String.valueOf(key), x, y);
    		}				
    	}
    }
    p.s: i'm not getting any errors.
    Last edited by Darwanism; 11-20-2012 at 11:21 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: keyboard listener not taking effect

    One issue, you call keyevent1.setFocusable(true) which allows the JPanel to accept the focus, but I don't see you requesting that the focus be given to this JPanel such as by calling keyevent1.requestFocusInWindow();

    Having said this, I want to advise you not to use KeyListeners if possible, especially if it requires the kludge of forcing the focus on something that really shouldn't have focus. Much better would be to use Key Bindings if this can be used for your project.

  3. #3
    Darwanism is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: keyboard listener not taking effect

    i'm a bit rusty but if i recall well only focused component can receive a keyEvent that's why i did that,still it doesn't explain why the listener isn't working i know i did few useless stuff like the font and all i'm just practicing for an upcoming exam

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

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

    Default Re: keyboard listener not taking effect

    Moved from New to Java.

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

  6. #6
    Darwanism is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: keyboard listener not taking effect

    problem solved thx again for replying :)

Similar Threads

  1. method not taking effect
    By Arbalest in forum New To Java
    Replies: 4
    Last Post: 10-19-2012, 08:21 PM
  2. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  3. Background Keyboard Listener
    By zisforzorro in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 05:18 PM
  4. jQuery effect
    By lilleza87 in forum New To Java
    Replies: 4
    Last Post: 01-11-2011, 02:36 PM
  5. fading effect
    By silversurfer2in in forum AWT / Swing
    Replies: 4
    Last Post: 06-11-2010, 03:23 PM

Posting Permissions

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