Note that there are some explanatory texts on larger screens.

plurals
  1. POJApplet/JPanel not receiving KeyListener events!
    text
    copied!<p>I cannot get my JPanel within my JApplet to receive keyboard events. I CANNOT FATHOM why!</p> <p><strong>Note that...</strong></p> <ol> <li><p>Clicking the panel (with mouse) before typing makes no difference. This is by far the most common advice I see given on the Net.</p></li> <li><p>I have tried using the 'low-level' java.awt.KeyEventDispatcher interface. That makes no different either!</p></li> <li><p>However, if I use Applet instead of JApplet, then the Applet DOES receive keyboard events. But even here, the moment I add a Panel to this Applet (the Panel is really where all my app/painting logic is), I once again stop receiving kb events (in my Panel)!</p></li> <li><p>Now, I cannot simply use Applet (instead of JApplet) because, among other things, its onPaint gets a Graphics (instead of a Graphics2D object). So, #3 is NOT a solution for me.</p></li> <li><p>Things work like a charm in AppletViewer that comes with JDK.</p></li> </ol> <p>I desperately need someone's help here. Spent last 2-3 days trying all kinds of permutations I don't even recall now.</p> <p><strong>My platform details:</strong></p> <ol> <li><p>Firefox 3.5.3</p></li> <li><p>Fedora 11 on x86 (with latest updates/patches)</p></li> <li><p>Java Plugin: tried both of these, made no difference.</p> <p>3.1 IcedTea Java Web Browser Plugin 1.6 (fedora-29.b16.fc11-i386)</p> <p>3.2 jdk1.6.0_16/jre/plugin/i386/ns7/libjavaplugin_oji.so</p></li> <li><p>Used the above jdk1.6.0_16 to compile my applet source.</p></li> </ol> <p>Here's my code. Will greatly appreciate to hear from my fellow programmers... as I'm completely stuck!</p> <p>Thanks,</p> <p>/SD</p> <pre><code>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(); // Panel related // Note: I'd like this red panel to handle // all my keyboard and mouse events. 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); // applet related // Note: Added this only for debugging. I do NOT want // to handle my mouse/kb events in the applet. 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) { } } </code></pre> <p>The HTML:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;body&gt; &lt;applet id="myApplet" code="TestApplet.class" width="425" height="150" &gt; &lt;/applet&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload