Get the item in the SWT list on Mouse Up(在 Mouse Up 上获取 SWT 列表中的项目)
问题描述
我想知道是否可以在鼠标上移时获取列表中的项目?我在网上搜索并找到了许多使用 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 列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Mouse Up 上获取 SWT 列表中的项目


- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01