Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

canada eh

macrumors regular
Original poster
Dec 22, 2009
117
0
Barrie, Ontario
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

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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?

Code:
public void init()
{
  requestFocus();
}
 

canada eh

macrumors regular
Original poster
Dec 22, 2009
117
0
Barrie, Ontario
Thanks for the reply

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

Code:
public void actionPerformed(ActionEvent e){
		if(e.getSource() == timer1){
		requestFocus();
                }
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.