将事件发布到事件队列/事件分派线程(EDT)- 事件未传递给actionPerformed()。

huangapple 未分类评论58阅读模式
标题翻译

Posting events to event queue / edt - events not passed to actionPerformed()

问题

我正在使用Java AWT事件队列进行一些测试发布事件

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

然后在我的JFrame中有一个事件监听器

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

问题是我在队列中发布的事件没有被分派到actionPerformed()监听器

我进行了进一步的测试
在我的类构造函数中
long mask = 0xffffffffffffffffL;
Toolkit.getDefaultToolkit().addAWTEventListener(this, mask);

事件监听器
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);
    }
}

我使用了一个HashMap因为收到的事件数量相当大因此我只是打印出唯一的事件
这一次我确实得到了响应
1001
EVENT
在事件监听器eventDispatched()但不在actionPerformed()监听器中
如果我从不同的线程中发布事件例如
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();

即使事件监听器也没有收到事件

我的问题是是否有任何方法可以将ACTION_PERFORMED事件发布到EDT事件分派器线程事件队列并在actionPerformed(ActionEvent e)或其他线程的监听器中处理它
提前谢谢

编辑我确实尝试过用自定义队列替换事件队列那样可以工作但是否有办法使用标准事件队列和EDT做同样的事情
英文翻译

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

ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, &quot;EVENT&quot;);
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() == &quot;EVENT&quot;) {
			System.out.println(&quot;Event received&quot;);
	}
}

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&lt;Integer, Integer&gt; seen = new HashMap&lt;&gt;();
	
@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, &quot;EVENT&quot;);
		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?

huangapple
  • 本文由 发表于 2020年5月30日 19:47:52
  • 转载请务必保留本文链接:https://java.coder-hub.com/62101965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定