2015-10-26 42 views
2

我有一個TableViewer水平滾動條。移動滾動條或重新調整窗口大小可以隱藏或顯示某些列。JFace TableViewer顯示列寬

我想知道某個列在滾動後是否可見,如果是,它的確切寬度是可見的。

任何方式來做到這一點?

回答

2

您需要查詢底層的Tableviewer.getTable())及其TableColumntable.getColumns())才能解決此問題。

如果您使用TableViewerColumn定義了查看者列,那麼也可以通過viewerColumn.getColumn()訪問這些列。

要確定最右側的可見列,可以使用表的clientArea(Table#getClientArea().width)的寬度,該寬度爲您提供顯示列的總可用空間。

每列的寬度爲TableColumn.getWidth()。添加所需的所有列的寬度,將使您能夠清楚地看到它是否可見。

table.getHorizontalBar().getSelection()爲您提供了行的水平偏移量。當減去這個偏移量時,如果給定的列是可見的,你應該能夠剝離它。

生成的代碼應該是這樣的:

boolean isColumnVisible(Table table, int columnIndex) { 
    int columnRight = 0; 
    for(int i = 0; i <= columnIndex; i++) { 
    columnRight += table.getColumn(i).getWidth(); 
    } 
    int clientAreaWidth = table.getClientArea().width; 
    int horizontalOffset = table.getHorizontalBar().getSelection(); 
    return columnRight - horizontalOffset <= clientAreaWidth; 
} 

注意,如果列可以被重新排序,你需要通過table.getgetColumnOrder()

+0

大詳盡的解答,以確定實際columnIndex!我只需要偏移量,因爲我的列在索引0處。非常感謝。 – 2c00L

相關問題