Get the item in the SWT list on Mouse Up

旧巷老猫 提交于 2019-12-01 20:16:28

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();
    }
  }
} );

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!