The Artima Developer Community
Sponsored Link

Java Answers Forum
How to add a keylistener to a JPanel

2 replies on 1 page. Most recent reply: Mar 7, 2002 2:22 PM by Julio C. Fonseca C.

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 2 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:00 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


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How to add a keylistener to a JPanel Posted: Mar 7, 2002 11:10 AM
Reply to this message Reply
Hi
By default, JPanel cannot gain focus. You need to override isFocusTraversable() method for your JPanel and then return true from this method. Now your JPanel can gain focus and hence can also receive key strokes and your key events will work fine. In the following example, I have used your JPanel with little modification and then I have added your JPanel inside a frame . Now when you compile and run this code then your key strokes will be handled by the your key event handlers defined in your JPanel and you can see messages being printed out on console. However, if you add more controls (e.g. a JButton) to the frame then you will have to tab to JPanel so that it has focus and then you can handle key strokes in JPanel.
Thanks
Kishori

////////////////MyFrame.java
import java.awt.event.* ;
import javax.swing.* ;

public class MyFrame extends JFrame {
Pizarron pp = new Pizarron ( ) ;
MyFrame ( ) {
getContentPane().add ( pp ) ;

setBounds ( 100, 100, 500, 500 ) ;
pack() ;
show() ;
}

public static void main( String[] args ) {
new MyFrame ( ) ;
}
}

class Pizarron extends JPanel {

Pizarron() {
super() ;
addKeyListener(new MyKeyListener());
}

// Override the isFocusTraversable method and return true
public boolean isFocusTraversable ( ) {
return true ;
}

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());
}
}
}

Julio C. Fonseca C.

Posts: 18
Nickname: jfonseca
Registered: Mar, 2002

Re: How to add a keylistener to a JPanel Posted: Mar 7, 2002 2:22 PM
Reply to this message Reply
Thanks Kishori!,

Now I'm just use the requestFocus method to gain the JPanel's focus and the problem is solved!,

Greetings
Julio

Flat View: This topic has 2 replies on 1 page
Topic: Crystal Reports Previous Topic   Next Topic Topic: A little help getting started!!

Sponsored Links



Google
  Web Artima.com   

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