2014-03-05 83 views
0

我遇到了SWT Table和JFace TableViewer的問題。 我需要知道在哪個列上執行了右鍵單擊。不幸的是,TableColumnTableViewerColumn不能識別右鍵單擊事件。獲取ScrolledForm中的x座標SWT

所以我發現這個「黑客」來檢查在屏幕上執行鼠標點擊的位置,獲取y座標並添加每個表列的寬度,只要該值小於y座標。

如果y座標位於添加的寬度之間,則最後添加的列是右鍵單擊的列。見下文。

int width = 0; // the start point. 0 only works if not scrolled 

    final TableColumn[] sorted = new TableColumn[table.getColumns().length]; 
    final int[] order = table.getColumnOrder(); 
    int ctn = 0; 
    for(final int i : order) 
    { 
     sorted[ctn] = table.getColumn(i); 
     ctn++; 
    } 

    for(int i = 0; i < table.getColumns().length; i++) 
    { 
     final TableColumn tc = sorted[i]; 


     if((width < point.x) && (point.x < (width + tc.getWidth()))) 
     { 

     rightClickedColumn = tc; // the colums is in the range, so this is the one clicked on 

     } 
     width += tc.getWidth(); // add the width of this columns so we can check the next one 
    } 

只要保持TableViewer的組合沒有滾動,這工作正常。

當它滾動時,0的起始值不再正確,它必須是原始0和從表格中可見的第一個像素之間的差值。 因此,如果100px的表格不可見,我想獲得這個100px。如何獲得滾動合成中第一個可見像素的位置?

希望你明白我的意思

編輯:我需要得到通知時,表列標題是右鍵單擊

+0

你這樣做是爲了顯示上下文(右鍵)菜單? –

+0

否。顯示上下文菜單。在這裏,你可以激活一個列。這個列表應該在執行了右鍵單擊之後添加。如上所述,這在沒有任何滾動的情況下起作用 – Mirco

回答

2

而不是使用TableColumn#getWidth()你應該TableItem#getBounds()工作,以確定列你在

下面是一個例子:

public static void main(String[] args) 
{ 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new FillLayout()); 

    ScrolledComposite comp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); 
    comp.setLayout(new FillLayout()); 

    final Table table = new Table(comp, SWT.BORDER | SWT.FULL_SELECTION); 
    table.setHeaderVisible(true); 
    table.setLinesVisible(true); 
    for (int i = 0; i < 4; i++) 
    { 
     TableColumn column = new TableColumn(table, SWT.NONE); 
     column.setText("Column " + i); 
    } 
    for (int i = 0; i < 64; i++) 
    { 
     TableItem item = new TableItem(table, SWT.NONE); 
     for (int j = 0; j < table.getColumnCount(); j++) 
     { 
      item.setText(j, "Item " + i + "-" + j); 
     } 
    } 
    for (int i = 0; i < table.getColumnCount(); i++) 
    { 
     table.getColumn(i).pack(); 
    } 
    table.addListener(SWT.MouseDown, new Listener() 
    { 
     public void handleEvent(Event event) 
     { 
      Point pt = new Point(event.x, event.y); 
      TableItem item = table.getItem(pt); 
      if (item == null) 
       return; 
      for (int i = 0; i < table.getColumnCount(); i++) 
      { 
       Rectangle rect = item.getBounds(i); 
       if (rect.contains(pt)) 
       { 
        int index = table.indexOf(item); 
        System.out.println("Item " + index + "-" + i); 
       } 
      } 
     } 
    }); 

    comp.setContent(table); 
    comp.setExpandHorizontal(true); 
    comp.setExpandVertical(true); 
    comp.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

    shell.pack(); 
    shell.setSize(400, 600); 
    shell.open(); 

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

它將顯示選定的表格單元格(行和列)。


UPDATE

如果你想監聽的表格標題點擊事件,更換一次for環路與此:

for (int i = 0; i < 4; i++) 
{ 
    final TableColumn column = new TableColumn(table, SWT.NONE); 
    column.setText("Column " + i); 
    column.addListener(SWT.Selection, new Listener() 
    { 
     @Override 
     public void handleEvent(Event arg0) 
     { 
      System.out.println(column.getText()); 
     } 
    }); 
} 

更新2

檢測在Table標題上點擊右鍵會有點棘手。有一個bug報告hereSWT.MenuDetect不能TableColumn小號

但是可以使用,有一種變通方法:

public static void main(String[] args) 
{ 
    final Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new FillLayout()); 

    ScrolledComposite comp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); 
    comp.setLayout(new FillLayout()); 

    final Table table = new Table(comp, SWT.BORDER | SWT.FULL_SELECTION); 
    table.setHeaderVisible(true); 
    table.setLinesVisible(true); 
    for (int i = 0; i < 4; i++) 
    { 
     final TableColumn column = new TableColumn(table, SWT.NONE); 
     column.setText("Column " + i); 
    } 
    for (int i = 0; i < 64; i++) 
    { 
     TableItem item = new TableItem(table, SWT.NONE); 
     for (int j = 0; j < table.getColumnCount(); j++) 
     { 
      item.setText(j, "Item " + i + "-" + j); 
     } 
    } 
    for (int i = 0; i < table.getColumnCount(); i++) 
    { 
     table.getColumn(i).pack(); 
    } 

    table.addListener(SWT.MenuDetect, new Listener() 
    { 
     @Override 
     public void handleEvent(Event event) 
     { 
      Point pt = display.map(null, table, new Point(event.x, event.y)); 
      Rectangle clientArea = table.getClientArea(); 
      boolean header = clientArea.y <= pt.y && pt.y < (clientArea.y + table.getHeaderHeight()); 

      if (header) 
      { 
       TableItem item = table.getItem(0); 
       for (int i = 0; i < table.getColumnCount(); i++) 
       { 
        Rectangle rect = item.getBounds(i); 
        if (pt.x >= rect.x && pt.x <= rect.x + rect.width) 
        { 
         System.out.println("Header " + i); 
        } 
       } 
      } 
     } 
    }); 

    comp.setContent(table); 
    comp.setExpandHorizontal(true); 
    comp.setExpandVertical(true); 
    comp.setMinSize(table.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

    shell.pack(); 
    shell.setSize(400, 600); 
    shell.open(); 

    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
    display.dispose(); 
} 
+0

這裏的問題是列標題沒有得到事件 – Mirco

+0

@verbose-mode更新了我的答案。 – Baz

+0

@ verbose-mode再次更新我的答案。現在它檢測到在'表'標題上點擊右鍵。 – Baz