JScrollPane 内的 JLayeredPane 内的 JTable - 如何让它工作?
我想将来自 JTable 的对象分层放置在其顶部,因此使用 JLayeredPane 似乎很自然。然而,让它正确地绘制,正确地做标题等是非常困难的。我该如何做到这一点,以便:
- 行标题在滚动时显示并匹配
- 列标题在滚动时显示并匹配
- 表格正确
- 绘制大小,不会弄乱一切
请注意,因为 JDesktopPane
扩展JLayeredPane
,这个问题的答案还允许您在JDesktopPane
后面有一个JTable
(或任何其他组件)。
I want to put objects coming out of a JTable, layered on top of it, so using a JLayeredPane seems natural. However, getting this to paint properly, do the headers properly etc is very hard. How do I do this so that:
- The Row headers appear and match up when it scrolls
- The column headers appear and match up when it scrolls
- The table paints properly
- resizing doesn't mess everything up
Note that because JDesktopPane
extends JLayeredPane
, the answers to this question also allow you to have a JTable
(or any other component) behind a JDesktopPane
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似但不相同的问题有助于:
Java - 是否可以将 JLayeredPane 放入 JScrollPane 中? 和 如何在 JTable 单元格中显示动画 和 使用 JScrollPane 和JLayeredPane。
要正确执行此操作,需要考虑三个单独的问题:大小调整、标题和 UI 更改。
调整大小
要正确滚动和绘制,
JScrollPane
需要知道其内部组件的大小和首选大小,在本例中为JLayeredPane
。但是您希望由表格设置大小,因为其他组件将浮动在表格顶部。在这种情况下,最简单的方法是将 JLayeredPane 将与大小相关的属性委托给JTable
,如下所示。如果您不希望 JTable 决定 JLayeredPane 的大小,则需要以其他方式确定它,表格的大小也是如此。两者都需要显式调用
setPreferredSize()
和setSize()
。标题
由于 JTable 不再是视口,因此您需要自己链接标题。以下代码将起作用:
UI
Also note that JTable does some nasty code in `configureEnclosingScrollPane()` and `configureEnclosingScrollPaneUI()`. If you want to get UI style changes to work properly, then you'll have to override these methods, but I haven't worked out how to do this yet.
代码示例
完整的代码示例可在 GitHub 上获取。
Similar but not identical questions which help:
Java - Is it possible to put a JLayeredPane inside JScrollPane? and How to display animation in a JTable cell and Swing GUI design with JScrollPane and JLayeredPane.
To do this properly there are three separate issues to consider: Sizing, Headers and UI changes.
Sizing
To scroll and paint properly the
JScrollPane
needs to know the size and preferred size of the component inside it, in this case theJLayeredPane
. But you want the size to be set by the table, as otherComponent
s will be floating on top of the table. In this case the easiest way is to make the JLayeredPane delegate size related properties to theJTable
as follows.If you didn't want the
JTable
to be the thing which determines the size of theJLayeredPane
then it needs to be determined in some other way, and so does the table's size. Both will needsetPreferredSize()
andsetSize()
called on them explicitly.Headers
As the JTable is no longer the viewport, you'll need to link the headers yourself. The following code will work:
UI
Also note that JTable does some nasty code in `configureEnclosingScrollPane()` and `configureEnclosingScrollPaneUI()`. If you want to get UI style changes to work properly, then you'll have to override these methods, but I haven't worked out how to do this yet.
Code Sample
The complete code sample is available over on GitHub.