[英]How to track mouse over on an item in an SWT combo box (hot tracking)
我有一個顯示各種項目的組合框。 我想根據鼠標指針下的項目更改工具提示文本。 由於我無法為每個項目設置工具提示,因此我需要更改組合框的工具提示。
如何獲取鼠標指針下的項目? 我可以設置一個事件來跟蹤鼠標移動,然后獲取鼠標的點,但是我找不到一種方法來獲取與點位置匹配的組合中的項目。
一棵樹有這個:
private class KeyTreeMouseMoveListener implements MouseMoveListener
{
@Override
public void mouseMove( MouseEvent event )
{
TreeItem item = tree.getItem( new Point( event.x, event.y ) );
}
}
但我找不到組合框的類似方法。
就像是:
private class ComboMouseMoveListener implements MouseMoveListener
{
@Override
public void mouseMove( MouseEvent event )
{
int index = combo.getIndex( new Point( event.x, event.y ) );
}
}
會好的:-)
好吧,這有點丑陋,也是一種不好的做法,所以不要在家里嘗試這個......
private class ComboModifyListener implements ModifyListener
{
@Override
public void modifyText( ModifyEvent e )
{
<code to get the selected item>
combo.setToolTipText( "item tooltip text" );
// ugly part
ivShell.getDisplay().setCursorLocation(
ivShell.getLocation().x + combo.getLocation().x + 15,
ivShell.getLocation().y + combo.getLocation().y + 35 );
}
}
因此,在用戶選擇一個項目后,我獲取該項目,設置組合工具提示,然后移動 cursor 使其位於組合框上方。 x + 15
和y + 35
是組合框左上角的 SWAG 偏移量。
是的,我知道在程序控制下移動 cursor 是不好的做法,但它確實顯示了所選項目的工具提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.