PDA

View Full Version : Java WindowFocusListener




canada eh
Apr 28, 2011, 08:07 PM
Hi, I am currently in a introducory programming class in highschool, and we are doing java applets.

For the assignments we are doing simple arcade games like brick breaker, but when we run them we have to click on the screen to focus the window in order to use the keyboard.

Is there something I can do to eliminate the need to click. I am not too sure but from googling I think I need to implement a WindowFocusListener but I am not sure how to do it just to focus on the window in these simple applets.

Thanks in advance :)



jiminaus
Apr 28, 2011, 10:13 PM
You would implement WindowFocusListener if you wanted to know when your window gains and loses focus. But that interface can't be used to control the focus.

I'm at work and don't have a Java development environment, but does adding this to your applet work?


public void init()
{
requestFocus();
}

canada eh
Apr 28, 2011, 10:42 PM
Thanks for the reply

That line did work, but only when I put it on the timer and not in init.

public void actionPerformed(ActionEvent e){
if(e.getSource() == timer1){
requestFocus();
}
}