java—交换swt表中两行的正确方法

von4xj4u  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(347)

我在软件中使用的是swt库,而不是eclipse。我需要在运行时交换通过数组填充的表swt中的两行。我使用了以下方法:
我拿阵列
交换两个元素(使用collection.swap)
我把table倒空了
我从数组中重新插入元素
我想知道是否有更好的方法,是否必须重新插入表中的所有元素?因为当有这么多行的时候,视觉效果是很明显的,这看起来是一种浪费,因为我想一次只交换两行。
代码如下:

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

谢谢大家

cxfofazt

cxfofazt1#

打个电话就行了 setText 以及 setData 在已更改的两个表项上。
你可以得到一个现有的 TableItem 从表中使用

TableItem item = table.getItem(index);

哪里 index 是表中的行。

相关问题