Archived

This topic is now archived and is closed to further replies.

EasyRaider

Java applet woes

Recommended Posts

After some extensive debugging I've finally isolated a serious bug, but I don't understand it. I'd be very grateful if someone could tell me what's wrong with this code.
import java.applet.Applet;
import java.awt.Label;
import java.awt.event.*;

public class MyTest extends Applet
{
	private Label myLabel;

	public void init()
	{
		myLabel = new Label ("test");
		add (myLabel);

		addKeyListener (new KeyboardHandler());
	}

	private class KeyboardHandler extends KeyAdapter
	{
		public void keyPressed (KeyEvent event)
		{
			myLabel.setText ("key pressed");
		}
	}
}
  
The label doesn't change when pressing a key, but I get an exception when closing. Exception occured during event dispatching: java.lang.IllegalStateException: Can't dispose InputContext while it's active ___________________ Aim for the horizon but watch your step [edited by - EasyRaider on November 6, 2002 3:42:36 PM]

Share this post


Link to post
Share on other sites
The Label is hogging the input so that the Applet doesn''t receive the key events. Try adding the key listener to the label, or reworking the design so that the key events end up where they are supposed to be.

Share this post


Link to post
Share on other sites
Thanks, adding the keylistener to a component in the applet did the trick.

In my design (a game), the key presses belong to the applet itself (the interface), but who cares?

Share this post


Link to post
Share on other sites