Java:在不触发双击的情况下检测三次单击 [英] Java : detect triple-click without firing double-click
问题描述
我有一个JTable,我想在其中双击单元格时调用一个函数,并在三次单击单元格时调用另一个函数.
I have a JTable in which I want to call a function when a cell is double-clicked and call another function when the cell is triple-clicked.
双击单元格时,我不想调用双击功能.
When the cell is triple-clicked I do not want to call the double-click-function.
我现在拥有的是(mgrdAlarm是JTable):
What I have right now is (mgrdAlarm is the JTable) :
mgrdAlarm.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
System.out.println("getClickCount() = " + e.getClickCount());
if (e.getClickCount()==2)
{
doubleClick();
System.out.println("Completed : doubleClick()");
}
if (e.getClickCount()==3)
{
tripleClick();
System.out.println("Completed : tripleClick()");
}
}
});
双击,控制台显示:
getClickCount() = 1
getClickCount() = 2
Completed : doubleClick()
三次单击后,控制台将显示:
When triple-clicked the console shows :
getClickCount() = 1
getClickCount() = 2
Completed : doubleClick()
getClickCount() = 3
Completed : tripleClick()
单击三次后,我希望控制台显示:
When triple-clicked I want the console to show :
getClickCount() = 1
getClickCount() = 2
getClickCount() = 3
Completed : tripleClick()
因此,我不想在单元格被三次单击时调用doubleClick()函数,但是我确实想在单元格被双击时调用doubleClick()函数.
So I do not want to call the function doubleClick() when the cell is triple-clicked, but I do want to call the function doubleClick() when the cell is double-clicked.
所有答复都表明,解决方案似乎是延迟双击动作,并等待一定时间才能进行三次单击.
As all replies suggest the solution seems to be to delay the double-click-action and wait a certain time for the triple-click.
但正如此处所讨论的那样,这可能会导致不同类型的问题: 用户可能将其双击时间设置得很长,这可能与我的三次单击超时重叠.
But as discussed here that might lead to a different type of problem : The user might have set his double-click-time quite long, which might overlap with the timeout of my triple-click.
如果在执行三次单击操作之前执行两次双击操作并没有真正的灾难,但是它确实会产生一些额外的开销,尤其是一些我想避免的额外数据流量.
It is no real disaster if my double-click-action is executed before my triple-click-action, but it does generate some extra overhead, and especially some extra data traffic which I would like to prevent.
由于到目前为止唯一的解决方案可能会导致其他问题,而实际上可能比原始问题更严重,所以我将其保留为当前状态.
As the only solution so far might lead to other problems, which might actually be worse than the original problem, I will leave it as it is right now.
推荐答案
先前的答案是正确的:您必须考虑时间并延迟将其识别为双击,直到经过一定时间为止.正如您所注意到的那样,挑战在于用户的双击阈值可能会很长或很短.因此,您需要知道用户的设置是什么.另一个堆栈溢出线程(在单个单击并在Java中双击)提到awt.multiClickInterval
桌面属性.尝试使用该阈值.
The previous answers are correct: you have to account for the timing and delay recognizing it as a double click until a certain amount of time has passed. The challenge is that, as you have noticed, the user could have a very long or very short double click threshold. So you need to know what the user's setting is. This other Stack Overflow thread ( Distinguish between a single click and a double click in Java ) mentions the awt.multiClickInterval
desktop property. Try using that for your threshold.
这篇关于Java:在不触发双击的情况下检测三次单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!