2010-05-01 45 views

回答

1

下面的鏈接是確定單元格是否可見的文章。你可以使用它 - 如果單元格可見,那麼該行是可見的。 (但當然,如果水平滾動也存在,可能不是整行。)

但是,我認爲當單元格比視口更寬時,這將失敗。要處理這種情況,可以更改測試以檢查單元格邊界的頂部/底部是否在視口的垂直範圍內,但忽略單元格的左/右部分。最簡單的方法是將矩形的左邊和寬度設置爲0.我還更改了只採用行索引的方法(不需要列索引),如果表不在視口中,它會返回true以更好地與您的使用案例相一致。

public boolean isRowVisible(JTable table, int rowIndex) 
{ 
    if (!(table.getParent() instanceof JViewport)) { 
     return true; 
    } 

    JViewport viewport = (JViewport)table.getParent(); 
    // This rectangle is relative to the table where the 
    // northwest corner of cell (0,0) is always (0,0) 

    Rectangle rect = table.getCellRect(rowIndex, 1, true); 

    // The location of the viewport relative to the table  
    Point pt = viewport.getViewPosition(); 
    // Translate the cell location so that it is relative 
    // to the view, assuming the northwest corner of the 
    // view is (0,0) 
    rect.setLocation(rect.x-pt.x, rect.y-pt.y); 
    rect.setLeft(0); 
    rect.setWidth(1); 
    // Check if view completely contains the row 
    return new Rectangle(viewport.getExtentSize()).contains(rect); 
} 
+0

那麼,這個解決方案至少已經把我在正確的道路上,這裏是爲我工作: 的JViewport視= scrollPane1.getViewport(); Rectangle rect = myTable.getCellRect(rowToSelect,1,true); if(!viewport.contains(rect.getLocation())) myTable.scrollRowToVisible(rowToSelect); 。 。 。非常感謝 – Brad 2010-05-01 14:59:31

相關問題