gpt4 book ai didi

java - 在强制滚动到它之前检查屏幕上是否出现一行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:35:13 25 4
gpt4 key购买 nike

我正在使用 Swing JTable,我想强制滚动到其中的特定行。这很简单,使用scrollRowToVisible(...),但我想先检查这一行在滚动到它之前在屏幕上是否已经可见,就好像它已经可见一样没有必要强制滚动。

我该怎么做?

最佳答案

下面的链接指向一篇确定单元格是否可见的文章。您可以使用它 - 如果单元格可见,则该行可见。 (当然,如果还存在水平滚动,则可能不是整行。)

但是,我认为当单元格比视口(viewport)宽时,这会失败。为了处理这种情况,您更改测试以检查单元格边界的顶部/底部是否在视口(viewport)的垂直范围内,但忽略单元格的左/右部分。将矩形的左侧和宽度设置为 0 是最简单的。我还更改了方法以仅获取行索引(不需要列索引),如果表是,它返回 true不在视口(viewport)中,这似乎更符合您的用例。

public boolean isRowVisible(JTable table, int rowIndex) 
{
if (!(table.getParent() instanceof JViewport)) {
return true;
}

JViewport viewport = (JViewport)table.getParent();
// This rectangle is relative to the table where the
// northwest corner of cell (0,0) is always (0,0)

Rectangle rect = table.getCellRect(rowIndex, 1, true);

// The location of the viewport relative to the table
Point pt = viewport.getViewPosition();
// Translate the cell location so that it is relative
// to the view, assuming the northwest corner of the
// view is (0,0)
rect.setLocation(rect.x-pt.x, rect.y-pt.y);
rect.setLeft(0);
rect.setWidth(1);
// Check if view completely contains the row
return new Rectangle(viewport.getExtentSize()).contains(rect);
}

关于java - 在强制滚动到它之前检查屏幕上是否出现一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2749977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com