JTable 转到行
我正在尝试实现 goto 函数。我有一个包含几千行的 JTable,我希望能够跳转到指定的行号。
else if (BUTTON_GOTO.equals(command))
{
int gotoLine = Integer.valueOf(gotoField.getText());
logTable.setRowSelectionInterval(gotoLine, gotoLine);
}
上面的代码将突出显示我正在查找的行,但不会跳转到它。有谁知道该怎么做?
谢谢
编辑 使用下面的解决方案时存在一个错误,应用程序会跳转到比所需行短的几行。有关更多详细信息,请参阅以下主题:
I am trying to implement a goto function. I have a JTable that has a few thousand rows and I want to be able to jump to a specified row number.
else if (BUTTON_GOTO.equals(command))
{
int gotoLine = Integer.valueOf(gotoField.getText());
logTable.setRowSelectionInterval(gotoLine, gotoLine);
}
The code above will highlight the row I am looking for, but does not jump to it. Does anyone know how to do this?
Thanks
EDIT
There is a bug using the solution below where the application jumps a few lines short of the desired line. See the topic below for further details:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
getCellRect() 返回一个将单元格限制在给定行和列的
矩形
,并且 scrollRectToVisible() 告诉表的父级(如果您使用的是 JScrollPane,则应该是 JViewPort ) 滚动到那里。Try:
getCellRect() returns a
Rectangle
which bounds the cell at the given row and column, and scrollRectToVisible() tells the table's parent (which should be a JViewPort if you are using a JScrollPane) to scroll there.我有一个类似的问题,我在上面的帮助下解决了
首先我添加了 gotoButton 和文本字段
,然后我为 gotoRowButton 添加了一个 actionListnener
这是我工作的视觉效果
显示 gotobutton 和文本字段以及 gotoRow 突出显示的图像
I have had a similar issue which i solved with help of above
first I added gotoButton and text field
then I added an actionListnener for the gotoRowButton
a visual of my work
An image showing gotobutton and textfield and highlight of gotoRow