Results 1 to 4 of 4
- 10-02-2012, 02:16 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 3
- Rep Power
- 0
Using KeyListener, as opposed to MouseListener
Hello. I have a program which I wrote as an example below:
Whenever I click in the window that pops up, It prints "Mouse Clicked"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) {} }
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?
- 10-02-2012, 02:50 AM #2
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.
- 10-02-2012, 07:06 AM #3
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-02-2012, 07:06 AM #4
Similar Threads
-
MouseListener for everything?
By DFTBA in forum New To JavaReplies: 2Last Post: 07-22-2012, 01:07 PM -
JLabel Dragging with Container as opposed to JLayeredPane
By 5myl in forum New To JavaReplies: 3Last Post: 01-02-2012, 05:35 AM -
Getting Child Nodes With XPath.evaluate on an Element as Opposed to a Node
By baiguai in forum XMLReplies: 5Last Post: 12-21-2011, 10:45 PM -
what is java logically as opposed to syntactically
By katon in forum New To JavaReplies: 10Last Post: 11-27-2009, 10:52 AM -
MouseListener
By Aswq in forum New To JavaReplies: 12Last Post: 07-18-2008, 08:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks