The Artima Developer Community
Sponsored Link

Java Answers Forum
How to add a keylistener to a JPanel

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

How to add a keylistener to a JPanel Posted: Mar 6, 2002 6:01 PM
Reply to this message Reply
Advertisement
Hi,

I'm trying to add a key listener to a component that explicity doesn't listen, I mean, I'm using a JPanel as a drawing area, well as a matter of fact, I want to hear the key events, I have tried the following cases,

public class Pizarron extends JPanel
{
JPanel areaDib;
Pizarron()
{
areaDib = new JPanel();
areaDib.addKeyListener(new MyKeyListener());
}

private class MyKeyListener extends KeyAdapter
{
public void keyPressed (KeyEvent e)
{
System.out.println(e.getKeyText (e.getKeyCode()));
}

public void keyReleased (KeyEvent e)
{
System.out.println(e.getKeyText(e.getKeyCode()));
}

public void keyTyped (KeyEvent e)
{
System.out.println(e.getKeyChar());
}
}


}

I've been doing the same listeners inner classes with Mouse Listener, and it works!, really it works in a very easy way, but,

what happened with non-key listener components?, is there some other special stuff that I have to do?


Thanks a lot
Julio Fonseca

Topic: Excel Spread Sheet data access Previous Topic   Next Topic Topic: java.lang.process.exec() and I/O.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use