Wierd problem with KeyListener and contentPane in applet.

A

apchar

Consider the following 2 applets. The first one works. The second
doesnt. It apparently cant get focus. The only difference is the use
of contentPanes. Do I need to do something special for JContainers
within contentPanes?
I'm using Java 1.4.2_02-b03 for linux. Incidently, both work fine on
Mac OSX.

/* <applet code="KeyTest2" width="500" height="300"></applet> */
// this one works
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyTest2 extends JApplet implements KeyListener
{
public void init() { addKeyListener(this); }
public boolean isFocusable() { return true; }
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {
System.out.println("ding!"); }
}


/* <applet code="KeyTest" width="500" height="300"></applet> */
// this one does not work in Linux.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyTest extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
contentPane.add(new KeyTestPanel());
}
}
class KeyTestPanel extends JPanel implements KeyListener
{
public KeyTestPanel() { addKeyListener(this); }
public boolean isFocusable() { return true; }
public void keyTyped(KeyEvent e) { System.out.println("ding");
}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
}
 
A

Andrew Hobbs

Not sure why you call it weird?

apchar said:
Consider the following 2 applets. The first one works. The second
doesnt. It apparently cant get focus.
True

The only difference is the use
of contentPanes. Do I need to do something special for JContainers
within contentPanes?

You proved to yourself in your first example that the Applet frame gets the
focus by default. Why are you expecting the panel to get the focus in your
second example.

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 
A

apchar

But even when I explicitly set the focus of the JPanel with
setFocus(true) it still wont respond. Also, replacing JPanel with
Canvas makes it work. Also, the JPanel has no such trouble with the
MouseListener. That qualiifies as wierd doesn't it? How do I force the
JPanel to gain focus? What should I be doing differently?
 
A

Andrew Hobbs

apchar said:
But even when I explicitly set the focus of the JPanel with
setFocus(true) it still wont respond.

The commands need to be given at the correct step in the sequence otherwise
the focus will be set back to the frame.
Also, replacing JPanel with
Canvas makes it work.

I am not sure about Canvas. That is an AWT component and the AWT components
probably don't behave and interact in the same way in terms of focus. (Sun
warn you not to mix the Swing and AWT components since it can have
unintended consequences).
Also, the JPanel has no such trouble with the
MouseListener.

Mouse Listeners do not require focus. Clicking on a component inherently
indicates which components should recieve the event anyway. In fact if you
implement a mouse listener on the JPanel, and get it to requestFocus(), then
clicking on it with the mouse will give it the focus and it will then
respond to key clicks.
That qualiifies as wierd doesn't it? How do I force the
JPanel to gain focus? What should I be doing differently?

Well on my system I can simply press Tab to traverse the focus onto the
panel and it then responds to key clicks.

If you want to do it programatically then I would suggest

http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html

Cheers

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top