I am running an applet, and am trying to focus on a component outside of the init method. I have tried using requestFocus and requestFocusInWindow. Both of these return true, as do isDisplayable, isVisible, and isFocusable, but calling hasFocus right after this returns false. The component just doesn’t show up in the applet.
Here is the method called by the event listener (when the user clicks ‘begin battle’), trying to focus on the component:
private void beginBattle(){
BattleSequence bs = new BattleSequence(..);
remove(pnlInit);
remove(pnlOOB);
remove(pnlBattle);
add(bs);
bs.setVisible(true);
bs.requestFocusInWindow(); //this returns 'true'
bs.setBounds(0, 0, bs.getSize().width, bs.getSize().height);
bs.init();
}
public class BattleSequence extends RavelSequence
public abstract class RavelSequence extends Applet implements KeyListener, FocusListener
Why is the focus request denied? If there is more code that is relevant, I can post it!
***EDIT:
Adding the mouse listener worked to get focus to the component, but I don’t want to have the user need a mouse to control the game. I changed the declaration of RavelSequence to extend Container instead of Applet, and everything works peachy.
bs.hasFocus still returns false, but the key events are handled appropriately.
Try to call it on event dispatch thread.