Java Swing - 了解 JViewport
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在寻找的方法是
在 JComponent 中定义的,在您的上下文中使用它
编辑:刚刚意识到您可能仍在摸不着头脑 - 尽管已经给出的所有答案在技术上都是正确的:-)
基本上,这都是关于坐标系统,即相对于给定原点的位置。当使用位置相关方法时,您必须了解该特定方法的坐标系,并且不能混合不同的系统(至少不能在不翻译一个或另一个的情况下)。
但是混合你做了:
解决方案是找到一个坐标系,其中上面找到的单元格位置有意义(或者手动将单元格位置翻译到父系统中,但这在这里不需要),有一些方法可以进行翻译:
查询表本身(而不是查询其父表)是更好的选择,因为它不需要任何有关其父表的知识。
The method you'r looking for is
it's defined in JComponent, in your context you use it
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:
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:
querying the table itself (as opposed to querying its parent) is preferable, because it doesn't need any knowlegde about its parent.
我相信包含与始终位于 (0,0) 的屏幕坐标有关,
我相信你想看看。
I believe contains relates to the on screen coordinates that are always at (0,0), You want to look at
i believe.