java 鼠标拖动_java 让表项支持鼠标拖动移位,并自动滚动滚动条

本文介绍如何在Java中实现表格控件的鼠标拖动功能,包括拖动过程中显示背景提示、自动滚动及移动表项的逻辑。通过监听dragOver事件改变滑过项的背景色,并在拖动至表的顶部或底部时自动滚动。同时,提供了实现移动选中项到当前位置、顶部或底部的方法。
摘要由CSDN通过智能技术生成

设想我们有一个表,表中有很多数据(多到竖起滚动条至少要出现)。我们需要一个功能:用鼠标拖动某些项,然后将他们移动另一个位置,例如我们可能想一些名字相似啊,或者是内容相关度更高的数据项在一起。

要支持鼠标拖动,显示要增加drag-drop的支持,不过我不打算详细介绍这个,可以找些其他的资料看看。

大致希望实现以下功能:

1.希望在拖过某项时,有一个背景色的回显,需要增加一个变量以记录当前滑过的项:

Java代码 a4c26d1e5885305701be709a3d33442f.png

privateTableItem prevItem;

private TableItem prevItem;

在dragOver()方法里设置滑过项的背景色,如下:

Java代码 a4c26d1e5885305701be709a3d33442f.png

// 当拖动滑过表项时,设置滑过项的背景色,以标示

if(prevItem !=null) {

prevItem.setBackground(null);

}

Widget item = event.item;

if(item !=null&& (iteminstanceofTableItem)) {

((TableItem) item).setBackground(getTable().getDisplay()

.getSystemColor(SWT.COLOR_GRAY));

}

prevItem = (TableItem) item;

// 当拖动滑过表项时,设置滑过项的背景色,以标示

if (prevItem != null) {

prevItem.setBackground(null);

}

Widget item = event.item;

if (item != null && (item instanceof TableItem)) {

((TableItem) item).setBackground(getTable().getDisplay()

.getSystemColor(SWT.COLOR_GRAY));

}

prevItem = (TableItem) item;

2.如果有竖直滚动条在,当拖动最上或最下时,如果滚动条可用,则滚动:

Java代码 a4c26d1e5885305701be709a3d33442f.png

// 如果竖直滚动条未出现,则不需要处理滚动

ScrollBar verticalBar = getTable().getVerticalBar();

intthumb = verticalBar.getThumb();

intmaximum = verticalBar.getMaximum();

if(maximum == thumb) {

return;

}

// 得到当前滚动条的位置

intselection = verticalBar.getSelection();

// 得到表项的高度

intitemHeight = getTable().getItemHeight();

// 得到表的绝对坐标位置

inty_table = getTable().toDisplay(0,0).y;

// 判断是向上滚动还是向下滚动,并处理

booleanisTop = event.y - y_table > itemHeight ?false:true;

if(isTop && selection >0) {

getTable().showItem(getTable().getItem(selection -1));

}elseif(prevItem ==null&& !isTop && selection + thumb 

getTable().showItem(getTable().getItem(selection + thumb +1));

}

// 如果竖直滚动条未出现,则不需要处理滚动

ScrollBar verticalBar = getTable().getVerticalBar();

int thumb = verticalBar.getThumb();

int maximum = verticalBar.getMaximum();

if (maximum == thumb) {

return;

}

// 得到当前滚动条的位置

int selection = verticalBar.getSelection();

// 得到表项的高度

int itemHeight = getTable().getItemHeight();

// 得到表的绝对坐标位置

int y_table = getTable().toDisplay(0, 0).y;

// 判断是向上滚动还是向下滚动,并处理

boolean isTop = event.y - y_table > itemHeight ? false : true;

if (isTop && selection > 0) {

getTable().showItem(getTable().getItem(selection - 1));

} else if (prevItem == null && !isTop && selection + thumb < maximum) {

getTable().showItem(getTable().getItem(selection + thumb + 1));

}

3.实现移动

Java代码 a4c26d1e5885305701be709a3d33442f.png

privatevoidmoveSelected(IStructuredSelection selection,

Widget currentItem,booleanisTop) {

if(selection.isEmpty()) {

return;

}

List continuousBlock = getFirstContinuousBlock(selection);

booleanhasMoved =false;

if(currentItem !=null) {

hasMoved = moveSelected2Current(continuousBlock,

(TableItem) currentItem);

}elseif(isTop) {

hasMoved = moveSelected2Top(continuousBlock);

}elseif(!isTop) {

hasMoved = moveSelected2Bottom(continuousBlock);

}

if(hasMoved) {

fireDragMovedEvent();

}

}

private void moveSelected(IStructuredSelection selection,

Widget currentItem, boolean isTop) {

if (selection.isEmpty()) {

return;

}

List continuousBlock = getFirstContinuousBlock(selection);

boolean hasMoved = false;

if (currentItem != null) {

hasMoved = moveSelected2Current(continuousBlock,

(TableItem) currentItem);

} else if (isTop) {

hasMoved = moveSelected2Top(continuousBlock);

} else if (!isTop) {

hasMoved = moveSelected2Bottom(continuousBlock);

}

if (hasMoved) {

fireDragMovedEvent();

}

}

这里的Field0是假设为表中存储的模型对象。

这里的hasMoved用于判断是否真的移动了项,如果移动了,发出一个事件通知。通常可以在这里定义一个移动监听列表,当移动时,发出通知,让监听实现者可以在移动后做一些事情。

移动的时候分移到最底、最顶或移到某个中间位置。

这里的getFirstContinuousBlock()方法用于得到一块连续选中区域:因为可能用户用鼠标加ctrl选中了很多非连续的区域,为了简单,我们只取第一个选中区,方法如下:

Java代码 a4c26d1e5885305701be709a3d33442f.png

privateList getFirstContinuousBlock(IStructuredSelection selection) {

List list = selection.toList();

intfirstId = Integer.parseInt(list.get(0).getId());

List result =newArrayList();

for(inti =0; i 

Field0 field0 = list.get(i);

intid = Integer.parseInt(field0.getId());

if(id == firstId) {

result.add(field0);

}else{

break;

}

firstId++;

}

returnresult;

}

private List getFirstContinuousBlock(IStructuredSelection selection) {

List list = selection.toList();

int firstId = Integer.parseInt(list.get(0).getId());

List result = new ArrayList();

for (int i = 0; i < list.size(); i++) {

Field0 field0 = list.get(i);

int id = Integer.parseInt(field0.getId());

if (id == firstId) {

result.add(field0);

} else {

break;

}

firstId++;

}

return result;

}

这个方法见仁见智。大家可以按自己的想法实现。

5.实现移动

最后就是实现三个对应的方法。也是见人见智。简单一些假如你的input是一个list或者是一个数组,那其实就是修改移动的对象在数组或者是list中的相对位置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值