0

i'm doing some tests with the java awt event queue, posting events.

ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "EVENT");
try {
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
} catch (SecurityException e) {
    e.printStackTrace();
}

Then in my JFrame, there is a event listener:

public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand() == "EVENT") {
            System.out.println("Event received");
    }
}

the trouble is the event i've posted in the queue did not get dispatched to the actionPerformed() listener.

i did further tests: in my class constructor:

    long mask = 0xffffffffffffffffL;
    Toolkit.getDefaultToolkit().addAWTEventListener(this, mask);

and the event listener:

private HashMap<Integer, Integer> seen = new HashMap<>();

@Override
public void eventDispatched(AWTEvent event) {
    if(seen.containsKey(event.getID()))         
        seen.replace(event.getID(), seen.get(event.getID())+1);
    else {
        System.out.println(event.getID());
        if(event instanceof ActionEvent)
            System.out.println(((ActionEvent) event).getActionCommand());
        seen.put(event.getID(), 1);
    }
}

i used a hashmap as the number of events received is rather overwhelming, hence i simply print the unique ones. this time round i did get a response

1001
EVENT

in the event listener eventDispatched(), but not in the action listener actionPerformed(). and if i were to post the event from a different thread e.g.

Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
        ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "EVENT");
        try {
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
        } catch (SecurityException e) {
            e.printStackTrace();
        }               
    }
});
thread.start();

even the event listener did not receive the event.

my question is then, is there any way that i can post an ACTION_PERFORMED event to the EDT (event dispatcher thread) event queue and have it processed in actionPerformed(ActionEvent e) or some such listeners from a different thread? Thanks in advance!

edit: i did try to replace the event queue with a custom queue, that works, but is there a way to do the same with the standard event queue and EDT?

  • Welcome to stackoverflow. Please do not include "thanks" or similar in questions. See here for more info: https://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts – PiRocks May 30 '20 at 11:54
  • 1
    `if(e.getActionCommand() == "EVENT") {` -- this is not how Strings should be compared since the `==` operation does a reference equality check which is not what you want. You want to check if the Strings have the same chars in the same order, which is what `.equals(...)` and `.equalsIgnoreCase(...)` does. – DontKnowMuchBut Getting Better May 30 '20 at 11:56
  • Better: `if ("EVENT".equals(e.getActionCommand()) {...}` – DontKnowMuchBut Getting Better May 30 '20 at 12:00
  • @DontKnowMuchButGettingBetter in this case the `String` comparison works, since OP is using literals which are interned. Not a cause of this particular problem anyway. And the question is closed for completely incorrect reasons. Which is a shame, because it's an interesting one. – pafau k. May 30 '20 at 12:08
  • If the OP posts a valid [mre] we can see if the question is a duplicate or not – DontKnowMuchBut Getting Better May 30 '20 at 12:41
  • Then maybe it's better to close it with a meaningful message so they may improve the question. Instead of selecting a random post and claiming this one is a duplicate of it, which does not help SO reputation. Especially with an unusualy well crafted first post otherwise. – pafau k. May 30 '20 at 21:38

0 Answers0