Java/Swing: sending an event to a specific component…

While working on the gui for simple rowLog last night I came across the problem of trying to send the keyboard input to a popup-menu to a text entry box. It turned out the dispatchEvent() method of Swing components doesn’t do what it says and dispatches the event to the keyboard manager instead of the component itself, which might reroute it somewhere else (if you’re unlucky it does an endless loop where the same event keeps getting sent to the same component over and over again, which tries to reroute it, but can’t). After fiddling about with reflection and other complicated things, simply in order to get dispatchEvent() to dispatch the event to the component on which the method is called, I discovered the simple solution of using:
KeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(boatEntry, arg0);

This, it turns out, actually sends the Event (arg0) to where you want it to go (boatEntry), in contrary to the dispatchEvent() method which claims to do this, but doesn’t. I.e. the following code DOESN’T work as specified:
boatEntry.dispatchEvent(arg0)

  1. No comments yet.

  1. No trackbacks yet.