Java Swing - 了解 JViewport

发布于 2024-10-31 12:26:37 字数 487 浏览 0 评论 0原文

我有一个带有 JScrollPane 的 JTable。我对scrollPane 视口有一些不明白的地方...我现在在表中选择行号1000,所以它上面的许多行在屏幕上不可见。现在,当我检查第 0 行在当前视口中是否可见时,它显示“是”。 这是我的代码:

    JViewport viewport = scrollPane1.getViewport();
    Rectangle rect = table1.getCellRect( 0, 1, true ); 

    // Check if view completely contains the row 0 :
    if( viewport.contains( rect.getLocation() ) )
        System.out.println( "The current view contains row 0" );

此代码始终返回 true,并且无论我站在哪一行,都会打印文本。我在这里错过了什么吗?

I have a JTable with a JScrollPane. There is something i do not understand with the scrollPane viewport ... I am now selecting row number 1000 in the table, so many rows above it are not visible on the screen. Now when i check if row 0 is visible in the current viewport, it says 'Yes'.
Here is my code :

    JViewport viewport = scrollPane1.getViewport();
    Rectangle rect = table1.getCellRect( 0, 1, true ); 

    // Check if view completely contains the row 0 :
    if( viewport.contains( rect.getLocation() ) )
        System.out.println( "The current view contains row 0" );

This code always returns true, and the text is printed, whatever the row i am standing on. Am i missing something here ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗心人 2024-11-07 12:26:37

您正在寻找的方法是

 getVisibleRect()

在 JComponent 中定义的,在您的上下文中使用它

 table1.getVisibleRect().contains(rect)

编辑:刚刚意识到您可能仍在摸不着头脑 - 尽管已经给出的所有答案在技术上都是正确的:-)

基本上,这都是关于坐标系统,即相对于给定原点的位置。当使用位置相关方法时,您必须了解该特定方法的坐标系,并且不能混合不同的系统(至少不能在不翻译一个或另一个的情况下)。

但是混合你做了:

      // cellRect is in table coordinates
      Rectangle cellRect = table.getCellRect(...)
      // WRONG!!! use table coordinates in parent sytem
      table.getParent().contains(cellRect.getLocation());

解决方案是找到一个坐标系,其中上面找到的单元格位置有意义(或者手动将单元格位置翻译到父系统中,但这在这里不需要),有一些方法可以进行翻译:

      // returns the visible part of any component in its own coordinates
      // available for all components
      Rectangle visible = table.getVisibleRect();
      // special service method in JViewport, returning the visible portion
      // of its single child in the coordinates of the child
      Rectangle viewRect = ((Viewport) (table.getParent()).getViewRect();
      // both are the same 
      visible.equals(viewRect)

查询表本身(而不是查询其父表)是更好的选择,因为它不需要任何有关其父表的知识。

The method you'r looking for is

 getVisibleRect()

it's defined in JComponent, in your context you use it

 table1.getVisibleRect().contains(rect)

edit: just realized that you probably are still scratching your head - even though all answers already given are technically correct :-)

Basically, it's all about coordinate systems, that is a location relative to a given origin. When using location related methods, you have to be aware of the coordinate system for that particular method is and you can't mix different systems (at least not without translating the one or other).

But mixing you did:

      // cellRect is in table coordinates
      Rectangle cellRect = table.getCellRect(...)
      // WRONG!!! use table coordinates in parent sytem
      table.getParent().contains(cellRect.getLocation());

The solution is to find a coordinate system where the cell location as found above makes sense (or translate the cell location into the parent system manually, but that's not needed here), there are methods which do the translation:

      // returns the visible part of any component in its own coordinates
      // available for all components
      Rectangle visible = table.getVisibleRect();
      // special service method in JViewport, returning the visible portion
      // of its single child in the coordinates of the child
      Rectangle viewRect = ((Viewport) (table.getParent()).getViewRect();
      // both are the same 
      visible.equals(viewRect)

querying the table itself (as opposed to querying its parent) is preferable, because it doesn't need any knowlegde about its parent.

梅倚清风 2024-11-07 12:26:37

我相信包含与始终位于 (0,0) 的屏幕坐标有关,

JViewPort.getViewRect().contains(rect.getLocation());

我相信你想看看。

I believe contains relates to the on screen coordinates that are always at (0,0), You want to look at

JViewPort.getViewRect().contains(rect.getLocation());

i believe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文