Ionut Nicu
Ionut Nicu

Reputation: 21

JApplet can't get focus when run from browser

I have some simple code where I'm trying to get keyboard events into a Java applet. The code runs just fine when being run with appletviewer, but when I'm loading it from a browser (tried both Chrome and Firefox), the JApplet won't get focus on click.

Trying exactly the same code with Applet instead of JApplet works without a problem.

Here's my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JApplet {
    String s = "";

    public void init() {
            setFocusable(true);
            setEnabled(true);
            addKeyListener(new KeyAdapter() {
                    @Override
                    public void keyPressed(KeyEvent e) {
                            s = "KEY PRESSED: " + e.getKeyCode();
                            repaint();
                    }

            });
            addMouseListener(new MouseAdapter() {
                     @Override
                     public void mousePressed(MouseEvent e) {
                             boolean ret = requestFocusInWindow();
                             s = "requestFocusInWindow: " + ret;
                             repaint();
                     }
            });
            requestFocusInWindow();
    }

    public void start() {
            requestFocusInWindow();
    }

    public void paint(Graphics g) {
            super.paint(g);
            requestFocusInWindow();
            g.setColor(Color.BLACK);
            s = "Focus owner: " + isFocusOwner() + ", " + s;
            g.drawString(s, 24, 24);
    }
}

Upvotes: 2

Views: 624

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

  • Applets should be created on Event Dispatch Thread by wrapping code in overridden init() method in SwingUtilities.invokeAndWait() block

  • Dont use KeyListener for JApplet/Swing components use KeyBindings

  • call requestFocusInWindow() on JApplet after creating and adding all content to container (this is not necessary with keybindings though)

  • Also dont do drawing in paint() rather add JPanel to container and override paintComponent(..)

Here is a small example, its a simple JLabel with a dummy label and textfield added to the container with a KeyBinding for A only; so when A is pressed it will be added to JLabel text:

enter image description here

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.AbstractAction;
import javax.swing.JApplet;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

public class Test extends JApplet {

    @Override
    public void init() {
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {

                    final JLabel label = new JLabel("Text:");
                    final JLabel label2 = new JLabel("Dummy label");
                    final JTextField jtf = new JTextField("Dummy Field");
                    label2.setFocusable(true);
                    label.setFocusable(true);

                    //allwos user to add letter A to JLabel
                    label.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "A");
                    label.getActionMap().put("A", new AbstractAction() {
                        @Override
                        public void actionPerformed(ActionEvent ae) {
                            String tmp = label.getText();
                            label.setText(tmp + "A");

                        }
                    });

                    setLayout(new GridLayout(3, 1));

                    add(label);
                    add(label2);
                    add(jtf);
                }
            });
        } catch (InterruptedException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }
}

Upvotes: 2

Related Questions