如何询问 JTable 中的选定行是否可见?

huangapple go评论86阅读模式
英文:

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);
  1. 如果所选行位于视口顶部以上,则视口将滚动,使行位于顶部。
  2. 如果所选行位于视口底部以下,则视口将滚动,使行位于底部。
  3. 如果所选行已在视口中可见,则不会执行滚动。
英文:

> 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);
  1. If the selected row is above the top of the viewport, the viewport will be scrolled so the row is at the top.
  2. If the selected row is below the viewport the viewport, will be scrolled so the row is at the bottom.
  3. If the selected row is already visible in the viewport, no scrolling is done.

huangapple
  • 本文由 发表于 2020年8月15日 21:36:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定