I'm having a Focus issue when running a Swing application.

There are 4 buttons visible on the JApplet.

I have an implementation that allows the user to make keyboard and mouse events in the JApplet.

The problem is that I can use the keyboard and mouse events when the program first starts, but the moment I click a button the focus is lost and the JApplet can no longer receive keyboard events.

What I'd like is a solution on how to reobtain the focus or somehow create a managed thread that will constantly re-focus the JApplet (or the contentpane) such that it will receive keyboard events from the user.

Thanks,

-Alex.

i don't know whether this will work, but try setting using .setFocusable(false) on the buttons. i don't think it will work, but it's worth a try

EDIT: it may ruin the way that you use the keyboard, but without seeing how you implement i cannot say

Comments
Yet another Daniweb Programming Prodigy

i don't know whether this will work, but try setting using .setFocusable(false) on the buttons. i don't think it will work, but it's worth a try

EDIT: it may ruin the way that you use the keyboard, but without seeing how you implement i cannot say

That worked!

Now, is there some kind of way to make the JApplet part of a focus-traversal between itself and the JButtons and additionally make it such that clicking a JButton will cause the JApplet to be focused after an action-performed?

That would be the best of both worlds, but it's not completely necessary. Thank you Sciwizeh, you genius! XD

If you can use them, key bindings are easier to handle with regards to focus:
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
but if you can't use those then you could call requestFocusInWindow() on the JApplet (or whatever other container you need to have focus) at the end of your button listener code

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;


public class FocusTest extends JApplet {
 
    JButton button;

    public void init() {
        button = new JButton("Ok");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                requestFocusInWindow();
            }
        });
        setLayout(new BorderLayout());
        add(button,BorderLayout.SOUTH);
        
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                JOptionPane.showMessageDialog(FocusTest.this, "Key pressed");
            }
            
        });
    }
}
Comments
You rock!

You both rock!

I didn't think I could directly treat a JApplet as a window - for some time I was trying to tie together a WindowListener into a JApplet but considering a JApplets methodology I guess that's impossible?

Even so, this method worked! I'll be making a new topic describing a different issue pertaining to JApplets and Windows--

-Alex

This question has already been answered. Start a new discussion instead.