JApplet / JPanel does not accept KeyListener events!

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

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

HTML:

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

, :

, KeyListener JApplet, KeyEventDispatcher.

public class AppletMain extends JApplet implements

java.awt.KeyEventDispatcher

, KeyboardFocusManager

KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);

dispatchKeyEvent :

@Override
public boolean dispatchKeyEvent(KeyEvent e);

KeyEvents KeyListener.

+3

, , JApplet.
setFocusable(true); .
, requestFocusInWindow();, .

+2

I had a problem with sun-java-6 packages and openjdk packages in both Ubuntu 9.04 and 10.10 with Firefox version 3.6.9 and 3.6.14. I found two workarounds: use the Applet instead of the JApplet or implement the MouseListener, which calls the requestFocus () function in the mousePressed (..) function.

+1
source

Source: https://habr.com/ru/post/1721675/


All Articles