JTable目前显示多少行? [英] How many rows is a JTable currently displaying?
问题描述
JTable有一个方法getVisibleRowCount(),它显示要显示的首选行数。
JTable has a method getVisibleRowCount(), which shows the preferred number of rows to display.
我想确定实际当前在JTable中可见的行数。我该怎么做?
I want to determine the actual number of rows currently visible in a JTable. How can I do this?
我目前的尝试是:
int rowsVisible = table.getSize().getHeight()/table.getRowHeight();
但它给我的价值远远高于我能看到的价值。
but the value it gives me is quite higher than what I can see.
例如,当有10行或11行可见时,这给我的结果是18。
For example, when there are 10 or 11 rows visible, this gives me a result of 18.
推荐答案
我没有对此进行过测试,但我希望 JTc
包含在 JScrollPane
中,您可以使用 getViewPort
向其滚动窗格查询其视口,并从视口中检索大小。
I haven't tested this, but I would expect when a JTable
is contained in a JScrollPane
, you can ask the scrollpane for its viewport using getViewPort
, and retrieve the size from the viewport.
如果划分那个高度与表的行高有关,你可能得到一个更好的估计
If you divide that height with the row height of the table, you might get a better estimation
final int pageSize =
(int) (table.getParent().getSize().getHeight() / table.getRowHeight());
非常接近
这篇关于JTable目前显示多少行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!