elecay
elecay

Reputation: 536

Using KeyboardFocusManager

I'm trying to add some KeyEventPostProcessor to a several popUp windows, for each pop up I show, like this:

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor() {

        public boolean postProcessKeyEvent(KeyEvent e) {
            //do something
            return true;
        }
    });

But the problem is that the "KeyEventPostProcessors will be notified in the order in which they were added" and I need the reverse order. Can I do that?

Upvotes: 3

Views: 2248

Answers (2)

Johan Sannemo
Johan Sannemo

Reputation: 316

You could create your own class implementing KeyEventPostProcessor, which you give all the other Processors to, and let your own class call the processors in the order you like (obviously only adding your own processor to the KeyboardFocusManager.) This will, however, only make it possible to return a single return value when called, instead of every Processor returning a value.

If that is a problem, then you may very well have to remove all the processors and re-add them. The KeyboardFocusManager has a method called getKeyEventPostProcessors() which will return all the processors. You could retrieve that list, iterate over it and remove the processors using removeKeyEventPostProcessor(), add your own first, and then again iterate over the list and add all the previous processors. This will be rather inefficient, however.

If you add all the processors at once but want the order reversed, you can temporarily save them to a list and then traverse the list in reverse order, adding them to the KeyboardFocusManager only when you already have all of the processors ready.

Other than the workarounds above, there doesn't seem to be any way in the Java API to actually make it send the events in reverse order.

Upvotes: 2

jzd
jzd

Reputation: 23629

This seems trivial, but add them in reverse order if that is the order that you need them to be notified.

(If you are adding them in a loop, just traverse it in reverse order, if you are adding them as calls from other classes, then store the additions until all your popups are finished and add the stored additions in reverse order)

Upvotes: 1

Related Questions