本文介绍了如何用Java以编程方式将MouseEvent激活到MouseListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 JTree
,带有自定义关联的 MouseListener
(用于显示弹出窗口等)。我需要触发一个 MouseEvent
,它将被 MouseListener
捕获。我应该如何以编程方式执行此操作?
解决方案
您可以创建自己的MouseEvent并循环遍历所有侦听器并进行调用。 / p>
例如:
MouseEvent me = new MouseEvent(tree,0 ,0,0,100,100,1,false);
for(MouseListener ml:tree.getMouseListeners()){
ml.mousePressed(me);
}
I have a JTree
with a custom associated MouseListener
(for showing popup etc.). I need to fire a MouseEvent
that will be caught by the MouseListener
. How should I do that programmatically?
解决方案
You could create your own MouseEvent and loop through all the listeners and make the call.
For example:
MouseEvent me = new MouseEvent(tree, 0, 0, 0, 100, 100, 1, false);
for(MouseListener ml: tree.getMouseListeners()){
ml.mousePressed(me);
}
这篇关于如何用Java以编程方式将MouseEvent激活到MouseListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!