我有一个位于 JScrollPane 内的 JTable。
在运行时根据应用程序中发生的事件将行添加到表中。 当向表中添加新行时,我想让滚动窗格滚动到表的底部。
对于 JList,[ensureIndexIsVisible][1]()
强制列表中的特定索引可见。 我正在寻找同样的东西,但寻找 JTable。 看起来我可能必须手动移动滚动窗格上的滚动视图,但我认为必须有一种更简单的方法。
I have a JTable that is within a JScrollPane.
Rows are added to the table at runtime based on events that happen in my application. I want to have the scoll pane scroll to the bottom of the table when a new row is added to the table.
For JLists There is the [ensureIndexIsVisible][1]()
that forces a particular index in the list to be visible. I'm looking for the same thing but for a JTable. It looks like I might have to manually move the scrolling view on the scroll pane but I figured there had to be an easier way.
发布评论
评论(5)
很简单,JTable也有scrollRectToVisible方法。 如果您愿意,您可以尝试这样的操作,以便在添加新记录时使滚动窗格转到底部:
其中 i 是最后添加的记录。
It's very easy, JTable has scrollRectToVisible method too. If you want, you can try something like this to make scrollpane go to to the bottom if a new record is added :
Where i is last added record.
请参阅此示例: http://www.exampledepot.com/egs/javax .swing.table/Vis.html
更新:链接现已过时,这里是代码(来自 http://smi-protege.stanford.edu/repos/protege/protege-core/trunk/src/edu /stanford/smi/protege/util/ComponentUtilities.java )
See this example : http://www.exampledepot.com/egs/javax.swing.table/Vis.html
update: the link is now obsolete, here is the code (from http://smi-protege.stanford.edu/repos/protege/protege-core/trunk/src/edu/stanford/smi/protege/util/ComponentUtilities.java )
JList 内部使用 scrollRectToVisible 并指定要滚动到的坐标。 我认为您必须为 JTable 重新编写类似的功能。
JList internally use scrollRectToVisible and specify the coordinates to scroll to. I think you will have to recode a similar functionality for JTable.
第一个答案效果很好,但所选行位于表格的底部。 所以我创建了这个修改版本:
现在选定的行位于表格的顶部。
The first answer works well, but the selected row gets positioned at the bottom of the table. So I created this modified version:
Now the selected row gets positioned at the top of the table.
在我看来,设置视口位置而不是滚动表格要容易得多。 以下是我的代码。
It seems to me a lot easier to set the viewport position instead of scrolling the table. Following is my code.