英文:
How to ask if selected row in JTable is visible?
问题
private static void autoScrollToRow(JTable table) {
JViewport viewport = (JViewport) table.getParent();
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
Rectangle visibleRectangle = viewport.getVisibleRect();
if (!visibleRectangle.contains(cellRectangle)) {
table.scrollRectToVisible(new Rectangle(cellRectangle.x, cellRectangle.y, (int) visibleRectangle.getWidth(), (int) visibleRectangle.getHeight()));
}
}
英文:
I have this function
private static void autoScrollToRow(JTable table) {
JViewport viewport = (JViewport) table.getParent();
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
Rectangle visibleRectangle = viewport.getVisibleRect();
table.scrollRectToVisible(new Rectangle(cellRectangle.x, cellRectangle.y, (int) visibleRectangle.getWidth(), (int) visibleRectangle.getHeight()));
}
and this function scrolls in that way that currently selected row is first in tableView. How to edit this function so it only do scrolling if selected row is out of sight?
(Something like if(table.sellectedRowVisibleOnScreen(true))
)
答案1
得分: 1
你的方法中已经有两个矩形可以进行测试。
private static void autoScrollToRow(JTable table) {
JViewport viewport = (JViewport) table.getParent();
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
Rectangle visibleRectangle = viewport.getVisibleRect();
if (!visibleRectangle.contains(cellRectangle)) {
int width = (int) visibleRectangle.getWidth();
int height = (int) visibleRectangle.getHeight();
table.scrollRectToVisible(new Rectangle(
cellRectangle.x, cellRectangle.y, width, height));
}
}
英文:
You already have two Rectangles in your method that you can test.
private static void autoScrollToRow(JTable table) {
JViewport viewport = (JViewport) table.getParent();
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
Rectangle visibleRectangle = viewport.getVisibleRect();
if (!visibleRectangle.contains(cellRectangle)) {
int width = (int) visibleRectangle.getWidth();
int height = (int) visibleRectangle.getHeight();
table.scrollRectToVisible(new Rectangle(
cellRectangle.x, cellRectangle.y, width, height));
}
}
答案2
得分: 1
> 如何编辑这个函数,使其在仅当所选行不可见时才执行滚动?
您不需要进行任何特殊操作。
这是 scrollToRect(...)
方法的默认行为(正如我在评论中解释的那样)。
您在方法中只需要:
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
table.scrollRectToVisible(cellRectangle);
- 如果所选行位于视口顶部以上,则视口将滚动,使行位于顶部。
- 如果所选行位于视口底部以下,则视口将滚动,使行位于底部。
- 如果所选行已在视口中可见,则不会执行滚动。
英文:
> How to edit this function so it only do scrolling if selected row is out of sight?
You don't do anything special.
This is the default behavior of the scrollToRect(...)
method (as I explained in my comment).
All you need in your method is:
Rectangle cellRectangle = table.getCellRect(table.getSelectedRow(), 0, true);
table.scrollRectToVisible(cellRectangle);
- If the selected row is above the top of the viewport, the viewport will be scrolled so the row is at the top.
- If the selected row is below the viewport the viewport, will be scrolled so the row is at the bottom.
- If the selected row is already visible in the viewport, no scrolling is done.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论