英文:
Same click action passing different parameters to mouse listener. Is it possible?
问题
以下是翻译好的内容:
我在对话框A中为选择JTable中的行添加了一个鼠标监听器,其代码如下:
resultTable.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
thePatientFinderController.jTableMouseReleased(evt);
}
});
以及被调用的方法:
public void jTableMouseReleased(java.awt.event.MouseEvent evt)
{
// 业务逻辑
}
我在另一个对话框B中也在使用同样的JTable。现在,如果我在对话框B中选择一行,我希望mouseReleased
执行不同的操作。
我尝试创建另一个带有不同参数的mouseReleased方法,如下:
resultTable.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
thePatientFinderController.jTableMouseReleased(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt, int a)
{
thePatientFinderController.jTableMouseReleased(evt,a);
}
});
但这并没有起作用,它只调用了第一个方法。所以,我陷入了困境。
那么,有没有办法调用第二个方法呢?是不是需要不同的逻辑?
结论:同一个按钮应该根据不同的对话框调用不同的mouseReleased事件方法。
英文:
I have a mouse listener for selecting a row in JTable
in a dialog box say A, which has the following code.
resultTable.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
thePatientFinderController.jTableMouseReleased(evt);
}
});
and the method, which is called.
public void jTableMouseReleased(java.awt.event.MouseEvent evt)
{
// Business logic
}
I am using the very same JTable
, in another dialog box say B. Now, if I select a row in this dialog box B, I want to have the mouseReleased
to perform different action.
I tried to have another mouseReleased method like this, with different parameters.
resultTable.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseReleased(java.awt.event.MouseEvent evt)
{
thePatientFinderController.jTableMouseReleased(evt);
}
public void mouseReleased(java.awt.event.MouseEvent evt, int a)
{
thePatientFinderController.jTableMouseReleased(evt,a);
}
});
It didn't work, it just called the first method. So, I am stuck here.
So, is there any way to call the second method? Are a different logic is needed?
Conclusion: The same button should call different mouseReleased event methods, based on different dialog boxes.
答案1
得分: 2
这不是那样运作的。请记住:这些监听器是在框架内调用的。该框架只知道基本的 MouseListener 接口。它完全不知道您在实现接口的类中有一些其他方法。
请自问:框架如何知道应该获取哪些其他参数,以及选择并传递哪个方法?!
换句话说:如果只有一个 MouseListener 实例,那么您放入该类/方法中的代码需要以某种方式获取进一步的上下文,以防根据该上下文需要执行不同的“事情”。
也就是说:要么您有一个获取上下文并决定如何处理的监听器,要么(可能是更清晰的设计):您实现不同的鼠标监听器,并根据上下文使用“匹配”的鼠标监听器。
英文:
It doesn't work that way. Remember: these listeners are called within a framework. That framework only knows the base MouseListener interface. It has no idea that you have some other methods in the class implementing the interface.
Ask yourself: how is the framework supposed to know which other parameters to acquire, and which method to pick and pass those parameters?!
In other words: if there is only one MouseListener instance, then the code that you put into that class/method needs to somehow fetch further context, in case it needs do different "things" based on that context.
Meaning: either you have ONE listener that fetches context, and decides what to do, or (probably the cleaner design): you implement different mouse listeners, and depending on context, the "matching" mouse listener gets used.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论