views:

501

answers:

1

Hello,

I cannot get my JPanel within my JApplet to receive keyboard events. I CANNOT FATHOM why!

Note that...

  1. Clicking the panel (with mouse) before typing makes no difference. This is by far the most common advice I see given on the Net.

  2. I have tried using the 'low-level' java.awt.KeyEventDispatcher interface. That makes no different either!

  3. 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)!

  4. 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.

  5. Things work like a charm in AppletViewer that comes with JDK.

I desperately need someone's help here. Spent last 2-3 days trying all kinds of permutations I don't even recall now.

My platform details:

  1. Firefox 3.5.3

  2. Fedora 11 on x86 (with latest updates/patches)

  3. Java Plugin: tried both of these, made no difference.

    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

  4. Used the above jdk1.6.0_16 to compile my applet source.

Here's my code. Will greatly appreciate to hear from my fellow programmers... as 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();

    // 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) { }
}

The HTML:

<html>
  <head>
  </head>
  <body>
    <applet id="myApplet" code="TestApplet.class"
        width="425"
        height="150" >
    </applet>
  </body>
</html>
+1  A: 

I'm having the exact same issue with my JApplet code. Of course, I'm also running Fedora 11 x86, but Firefox 3.5.2 with Iced Tea

Craig
I appeal to all to help find a solution to this problem. Don't know who to talk to... FF folks, Java folks, Fedora folks, my keyboard folks...? :-(
Harry
You could upvote the question, so that it at least gets some attention.
Harry