I cannot get my JPanel in my JApplet to receive keyboard events. I CAN'T LOSE why!
Note that...
Clicking on the panel (with the mouse) before entering it does not matter. This is by far the most common tip I see on the Internet.
I tried using the low level java.awt.KeyEventDispatcher interface. This is also no different!
However, if I use Applet instead of JApplet, then Applet DOES receives keyboard events. But even here, when I add a panel for this applet (the panel really has all my applications / drawing logic), I again stop receiving kb events (in my panel)!
Now I can’t just use the Applet (instead of the JApplet), because, among other things, its onPaint gets Graphics (instead of the Graphics2D object). So, No. 3 is NOT a solution for me.
Things work like a charm in the AppletViewer that comes with the JDK.
I desperately need help here. I stayed the last 2-3 days, having tried all kinds of permutations, which I don’t even remember.
Details of my platform:
Firefox 3.5.3
Fedora 11 on x86 (with the latest updates / patches)
Java Plugin: tried both of them, it didn't matter.
3.1 IcedTea Java Web Browser Plugin 1.6 (fedora-29.b16.fc11-i386)
3.2 jdk1.6.0_16 / jre / plugin / i386 / ns7 / libjavaplugin_oji.so
Used above jdk1.6.0_16 to compile my applet source.
Here is my code. I would be very grateful for what I heard from my fellow programmers ... since I'm completely stuck!
Thanks,
/ SD
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class MyAppletKeyListener implements KeyListener, MouseListener {
public void keyPressed(KeyEvent e) {
System.out.println("panel:keyPressed" + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
System.out.println("panel:keyTyped" + e.getKeyChar());
}
public void mouseClicked(MouseEvent e) {
System.out.println("panel:mouseClicked");
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
}
public class TestApplet extends JApplet implements MouseListener {
public void init() {
System.out.println("applet:init");
MyAppletKeyListener listener = new MyAppletKeyListener();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(new JButton("test"));
panel.add(new JButton("test2"));
panel.setFocusable(true);
panel.requestFocus();
panel.setBackground(new Color(200, 0, 0));
panel.addKeyListener(listener);
panel.addMouseListener(listener);
addMouseListener(this);
getContentPane().setLayout(new FlowLayout());
getContentPane().add(panel);
}
public void mouseClicked(MouseEvent e) {
System.out.println("applet:mouseClicked");
}
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
}
HTML:
<html>
<head>
</head>
<body>
<applet id="myApplet" code="TestApplet.class"
width="425"
height="150" >
</applet>
</body>
</html>