2012-08-09 31 views
2

JTable有一個方法getVisibleRowCount(),它顯示要顯示的行數首選的當前顯示的JTable有多少行?

我想確定實際當前在JTable中可見的行數。我怎樣才能做到這一點?

我現在的嘗試是:

int rowsVisible = table.getSize().getHeight()/table.getRowHeight(); 

,但它給我的價值比我能看到相當高。

例如,當有10個或11行可見,這給了我的結果是18

回答

1

我沒有測試過這一點,但我希望當JTable包含在JScrollPane中,您可以使用getViewPort向滾動窗格提問它的視口,並從視口檢索大小。

如果分割與表的行高度高,你可能會得到更好的估計

final int pageSize = 
    (int) (table.getParent().getSize().getHeight()/table.getRowHeight()); 

是非常接近

+0

抱歉......但是a)不完整(正如@Mikle Garin已經提到的)並且太低級(總是使用可用的最高抽象,正如你所知道的那樣;-) – kleopatra 2012-08-10 10:41:11

+0

@SteveMcLeod在這個線程中有更好的答案。如果你接受的這個答案,我可以刪除這一點,因爲這是不正確的 – Robin 2012-08-10 11:32:12

-4

嘗試:

table.getModel().getRowCount(); 
+0

返回行的總#在表中,而不是當前可見行的#。 – 2012-08-09 16:22:25

+0

對不起我的壞。它可能與JScrollPane有關。 – jadrijan 2012-08-09 16:25:41

+0

投票刪除此答案,因爲它不回答提問。 – 2012-08-10 05:54:35

0

方式羅賓提供的是不完全正確 - 它贏得當桌子有不同的行高時工作。它也不檢查行間距和其他一些細微的差別。

這裏是將與任何L- & F和表設置工作例如:

public static void main (String args[]) 
{ 
    final JLabel rows = new JLabel ("Visible rows: ?", JLabel.CENTER); 

    final JTable table = new JTable (new DefaultTableModel (30, 3) 
    { 
     public String getColumnName (int column) 
     { 
      return "title"; 
     } 

     public Object getValueAt (int row, int column) 
     { 
      return "cell"; 
     } 
    }); 

    JScrollPane scroll = new JScrollPane (table) 
    { 
     public Dimension getPreferredSize() 
     { 
      Dimension ps = super.getPreferredSize(); 
      ps.height = 150; 
      return ps; 
     } 
    }; 
    scroll.addComponentListener (new ComponentAdapter() 
    { 
     public void componentResized (ComponentEvent e) 
     { 
      Rectangle vr = table.getVisibleRect(); 
      int visibleRows = 0; 
      for (int i = 0; i < table.getRowCount(); i++) 
      { 
       Rectangle cell = table.getCellRect (i, 0, false); 
       if (cell.y <= vr.y && cell.y + cell.height >= vr.y || 
         cell.y <= vr.y + vr.height && 
           cell.y + cell.height >= vr.y + vr.height || 
         cell.y >= vr.y && cell.y + cell.height <= vr.y + vr.height) 
       { 
        visibleRows++; 
       } 
      } 
      rows.setText ("Visible rows: " + visibleRows); 
     } 
    }); 

    JPanel panel = new JPanel (new BorderLayout (25, 25)); 
    panel.setBorder (BorderFactory.createEmptyBorder (25, 25, 25, 25)); 
    panel.add (rows, BorderLayout.NORTH); 
    panel.add (scroll, BorderLayout.CENTER); 

    JFrame frame = new JFrame(); 
    frame.add (panel); 
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    frame.pack(); 
    frame.setLocationRelativeTo (null); 
    frame.setVisible (true); 
} 

只要嘗試調整表格看效果。

此外,您可以修改「is cell visible」條件以排除(例如)具有少於5個像素(或其高度的一半)可見的行。

+0

附近..但仍然太低級(無需環路,只問:-) – kleopatra 2012-08-10 10:41:48

+0

表@kleopatra你將不得不循環,如果你想排除只有幾個可見像素的單元格,或者如果您正在使用一些複雜的表格(如Jide包中的表格)。例如 - 一些樹表中的某些行崩潰,在這種情況下你的aproach將無法正常工作;) – 2012-08-10 13:18:00

+0

它必須 - 如果它不表實現車;-)看不到任何理由要排除行低於給定的高度 - 要麼是實際的數量要麼。 – kleopatra 2012-08-10 13:26:56

8

問表,它做所有的辛勤工作的你:

Rectangle vr = table.getVisibleRect(); 
int first = table.rowAtPoint(vr.getLocation()); 
vr.translate(0, vr.height); 
int visibleRows = table.rowAtPoint(vr.getLocation()) - first; 
+0

'getVisibleRect' ......我知道有在'JTable'的方法,但我對*查看*搜索沒有任何了良好的效果。對此答案滿意,並且需要檢查我是否可以刪除我接受的答案 – Robin 2012-08-10 11:31:34

1

正是這些沒有嘗試是正確的。簡單地說,因爲它不關心滾動...: -/

我寫了基於正確答案的代碼,我找到了正確的答案,試過並且是正確的(我希望)。

但我想它僅列0 ...

public static int getNumberOfVisibleRows(JTable table) { 
    Rectangle vr = table.getVisibleRect(); 
    int first = table.rowAtPoint(vr.getLocation()); 
    vr.translate(0, vr.height); 
    return table.rowAtPoint(vr.getLocation()) - first; 
} 

public static void scrollToVisible(JTable table, int rowIndex, int columnIndex, int numberOfVisibleRows) { 
    Rectangle visibleRect = table.getVisibleRect(); 
    Rectangle rect1 = table.getCellRect(rowIndex, columnIndex, false); 
    if (visibleRect.y > rect1.y) { 
     table.scrollRectToVisible(rect1); 
    } else { 
     Rectangle rect2 = table.getCellRect(rowIndex + numberOfVisibleRows, columnIndex, false); 
     int width = rect2.y - rect1.y; 
     table.scrollRectToVisible(new Rectangle(rect1.x, rect1.y, rect1.width, rect1.height + width)); 
    } 
}