在SWT表中交换两行的正确方法



我在软件中使用SWT库,而不是在Eclipse中。我需要在运行时交换通过数组填充的表SWT中的两行。我使用了以下方法:

  1. 我取数组
  2. 交换两个元素(使用Collection.swap(
  3. 我清空了桌子
  4. 我重新插入阵列中的元素

我想知道是否有更好的方法,我必须重新插入表中的所有元素吗?因为当有这么多行时,视觉效果是明显的,这似乎是一种浪费,因为我想一次只交换两行。

这是代码:

if(table.getSelectionIndices()[0]<table.getItemCount()&&table.getSelectionIndices()[0]>0) {
Collections.swap(pluginsTmp, table.getSelectionIndices()[0], table.getSelectionIndices()[0]-1);
int nextSelectionIndex=table.getSelectionIndices()[0]-1;
updateTable();
table.setSelection(nextSelectionIndex);
}

在这种情况下;向上";选择的项目,与以上的项目交换

public void updateTable() {
table.removeAll();
for(int a=0;a<pluginsTmp.size();a++) {
Plugin p = pluginsTmp.get(a);
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0,p.getClass().getName().toString());
item.setText(1,p.getVersionMajor()+"."+p.getVersionMinor()+"."+p.getVersionBuild());
item.setText(2,p.getAuthor());
item.setText(3,(p.isEnabled())?"Enabled":"Disabled");
item.setData("className",p.getClass().getName().toString());
item.setData("status",(p.isEnabled())?"1":"0");
}
}

谢谢大家

只需对已更改的两个表项调用setTextsetData

您可以使用从表中获取现有的TableItem

TableItem item = table.getItem(index);

其中CCD_ 4是表中的行。

最新更新