在 Mouse Up 上获取 SWT 列表中的项目

2023-02-21Java开发问题
4

本文介绍了在 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 列表中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13