JTable有一個方法getVisibleRowCount(),它顯示要顯示的行數首選的。當前顯示的JTable有多少行?
我想確定實際當前在JTable中可見的行數。我怎樣才能做到這一點?
我現在的嘗試是:
int rowsVisible = table.getSize().getHeight()/table.getRowHeight();
,但它給我的價值比我能看到相當高。
例如,當有10個或11行可見,這給了我的結果是18
JTable有一個方法getVisibleRowCount(),它顯示要顯示的行數首選的。當前顯示的JTable有多少行?
我想確定實際當前在JTable中可見的行數。我怎樣才能做到這一點?
我現在的嘗試是:
int rowsVisible = table.getSize().getHeight()/table.getRowHeight();
,但它給我的價值比我能看到相當高。
例如,當有10個或11行可見,這給了我的結果是18
我沒有測試過這一點,但我希望當JTable
包含在JScrollPane
中,您可以使用getViewPort
向滾動窗格提問它的視口,並從視口檢索大小。
如果分割與表的行高度高,你可能會得到更好的估計
final int pageSize =
(int) (table.getParent().getSize().getHeight()/table.getRowHeight());
是非常接近
嘗試:
table.getModel().getRowCount();
返回行的總#在表中,而不是當前可見行的#。 – 2012-08-09 16:22:25
對不起我的壞。它可能與JScrollPane有關。 – jadrijan 2012-08-09 16:25:41
投票刪除此答案,因爲它不回答提問。 – 2012-08-10 05:54:35
方式羅賓提供的是不完全正確 - 它贏得當桌子有不同的行高時工作。它也不檢查行間距和其他一些細微的差別。
這裏是將與任何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個像素(或其高度的一半)可見的行。
問表,它做所有的辛勤工作的你:
Rectangle vr = table.getVisibleRect();
int first = table.rowAtPoint(vr.getLocation());
vr.translate(0, vr.height);
int visibleRows = table.rowAtPoint(vr.getLocation()) - first;
'getVisibleRect' ......我知道有在'JTable'的方法,但我對*查看*搜索沒有任何了良好的效果。對此答案滿意,並且需要檢查我是否可以刪除我接受的答案 – Robin 2012-08-10 11:31:34
正是這些沒有嘗試是正確的。簡單地說,因爲它不關心滾動...: -/
我寫了基於正確答案的代碼,我找到了正確的答案,試過並且是正確的(我希望)。
但我想它僅列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));
}
}
抱歉......但是a)不完整(正如@Mikle Garin已經提到的)並且太低級(總是使用可用的最高抽象,正如你所知道的那樣;-) – kleopatra 2012-08-10 10:41:11
@SteveMcLeod在這個線程中有更好的答案。如果你接受的這個答案,我可以刪除這一點,因爲這是不正確的 – Robin 2012-08-10 11:32:12