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.

Recommended Answers

All 5 Replies

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

commented: Yet another Daniweb Programming Prodigy +3

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

i don't really know, i assume you have read the sun tutorial on focus, but i don't see how you include an applet, being the root, i would assume it's possible, but i cannot find anything else

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");
            }
            
        });
    }
}
commented: You rock! +3

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.