0

在我的应用程序中,我有 2 个表,表 A 包含表 B 的可选列。应该可以将“列”(表 A 的表项)拖放到表 B 中。表 B 应该使用拖动的表项作为新列。这工作正常。表 B 附加了它们。

现在表 B 应该以正确的顺序添加列。org.eclipse.swt.dnd.DropTargetEvent 知道它的位置 (DropTargetEvent.x / y)。所以我必须找出放置位置的列/ columnIndex,所以我可以在 column.atPoint(x,y) 旁边添加“新列”。org.eclipse.swt.widgets.Table 本身刚刚获得了一个名为 getColumn(int index) 的方法。有没有办法解决这个问题?

4

1 回答 1

1

这是一些将打印出鼠标单击事件的列的代码。您可以修改它以使用放置的位置而不是鼠标单击:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Stackoverflow");
    shell.setLayout(new RowLayout(SWT.VERTICAL));

    Table table = new Table(shell, SWT.BORDER);
    table.setHeaderVisible(true);

    for(int col = 0; col < 3;  col++)
    {
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Col: " + col);
    }

    for(int row = 0; row < 20; row++)
    {
        TableItem item = new TableItem(table, SWT.NONE);

        for(int col = 0; col < table.getColumnCount();  col++)
        {
            item.setText(col, row + " " + col);
        }
    }

    for(int col = 0; col < table.getColumnCount();  col++)
    {
        table.getColumn(col).pack();
    }

    table.addListener(SWT.MouseDown, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Table table = (Table) e.widget;

            System.out.println("Column: " + getColumn(table, e.x));
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

private static int getColumn(Table table, int x)
{
    int overallWidth = 0;

    for(int i = 0; i < table.getColumnCount(); i++)
    {
        overallWidth += table.getColumn(i).getWidth();
        if(x < overallWidth)
        {
            return i;
        }
    }

    return -1;
}
于 2013-10-25T13:30:14.633 回答