2013-10-25 153 views
0

在我的应用程序中,我得到了2个表,表A包含表B的可选列。应该可以拖动“列”(表A的tableItem)并将它们放到表B中。表B应该使用拖动的tableItem作为新列。这工作正常。表B附加它们。org.eclipse.swt.widgets.TableColumn atPosition(x,y)

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

回答

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; 
} 
+0

我可能已经知道它,循环遍历所有列。我想也许还有另一种方式。非常感谢 – user2919816

+0

@ user2919816 AFAIK没有内置的方法来做到这一点。无论如何,请记住,任何内置解决方案都可能只是遍历所有列。如果您对我的回答感到满意,请考虑接受它。 – Baz

相关问题