问题描述
我想知道是否可以在鼠标上移时获取列表中的项目?我在网上搜索并找到了许多使用 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.
推荐答案
List#getItemHeight()
返回一个item所占区域的高度.有了这些信息和 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 )
的单列表来模拟列表小部件.该表提供了更好的开箱即用拖放支持.
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.
这篇关于在 Mouse Up 上获取 SWT 列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!