Results 1 to 6 of 6
- 11-20-2012, 11:18 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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
p.s: i'm not getting any errors.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); } } }Last edited by Darwanism; 11-20-2012 at 11:21 PM.
-
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.
- 11-20-2012, 11:39 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
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
-
Re: keyboard listener not taking effect
Please re-read my answer. I explained why your code didn't work.
- 11-21-2012, 04:37 AM #5
Re: keyboard listener not taking effect
Moved from New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-21-2012, 11:15 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
method not taking effect
By Arbalest in forum New To JavaReplies: 4Last Post: 10-19-2012, 08:21 PM -
Action-Listener with multiple Arguments? (Button Listener, specifically)
By Kevinw778 in forum AWT / SwingReplies: 2Last Post: 12-11-2011, 10:44 PM -
Background Keyboard Listener
By zisforzorro in forum New To JavaReplies: 2Last Post: 09-28-2011, 05:18 PM -
jQuery effect
By lilleza87 in forum New To JavaReplies: 4Last Post: 01-11-2011, 02:36 PM -
fading effect
By silversurfer2in in forum AWT / SwingReplies: 4Last Post: 06-11-2010, 03:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks