获取鼠标向上的SWT列表中的项目 [英] Get the item in the SWT list on Mouse Up
问题描述
我想知道是否可以在鼠标悬停列表中获取该项目?我在线搜索,并使用X和Y坐标找到了SWT表的许多示例,但没有使用列表。我基本上在做的是实现一个列表,通过拖放可以更改项目的顺序。为此,我需要能够将项目放在放置位置,以便我可以用拖动的项目交换该项目。
I wanted to know if its possible to get the item in the list on mouse up? I searched online and found many examples for SWT table using the X and the Y coordinate but none using a list. What I am basically doing is implementing a list in which the order of the items can be changed by drag and drop. For this I need to be able to get the item under the drop location so that I can swap that item with the dragged item.
推荐答案
p> 列表#getItemHeight()
返回一个项目占据的区域的高度。使用该信息和 getTopIndex()
,您应该能够在给定的x和y坐标上计算项目。
List#getItemHeight()
returns the height of the area one item occupies. With that information and getTopIndex()
you should be able to compute the item at a given x and y coordinate.
list.addListener( SWT.MouseDown, new Listener() {
@Override
public void handleEvent( Event event ) {
int itemTop = 0;
for( int i = 0; i < list.getItemCount(); i++ ) {
if( event.y >= itemTop && event.y <= itemTop + list.getItemHeight() ) {
System.out.println( "Click on item " + list.getItem( list.getTopIndex() + i ) );
}
itemTop += list.getItemHeight();
}
}
} );
或者,您可以使用带有 setHeaderVisible(false)的单列表/ code>来模拟列表窗口小部件。该表提供了更好的拖放支持。
Alternatively you could use a single-columned table with setHeaderVisible( false )
to emulate a list widget. The table provides better drag and drop support out of the box.
这篇关于获取鼠标向上的SWT列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!