简体   繁体   English

Java Swing - 了解JViewport

[英]Java Swing - Understanding the JViewport

I have a JTable with a JScrollPane. 我有一个带JScrollPane的JTable。 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. 使用scrollPane视口有一些我不理解的东西......我现在在表中选择行号1000,因此屏幕上看不到它上面的许多行。 Now when i check if row 0 is visible in the current viewport, it says 'Yes'. 现在,当我检查当前视口中是否显示第0行时,它会显示“是”。 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. 此代码始终返回true,并且无论我站在哪一行,都会打印文本。 Am i missing something here ? 我错过了什么吗?

The method you'r looking for is 你要找的方法是

 getVisibleRect()

it's defined in JComponent, in your context you use it 它在JComponent中定义,在您使用它的上下文中

 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. 查询表本身(而不是查询其父级)是可取的,因为它不需要任何关于其父级的知识。

I believe contains relates to the on screen coordinates that are always at (0,0), You want to look at 我认为包含与屏幕坐标有关,总是在(0,0),你想看

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

i believe. 我相信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM