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