Skip to main content
gamedev.net
🔒 Locked

Java applet woes

Started by EasyRaider 1 reply 500+ views
Original Post
EasyRaider
EasyRaider
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]
-------------------Our only true limitis our imaginationAim for the horizonbut watch your step
SabreMan
SabreMan
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.
EasyRaider
EasyRaider
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?
-------------------Our only true limitis our imaginationAim for the horizonbut watch your step

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.