JScrollPane 内的 JLayeredPane 内的 JTable - 如何让它工作?

发布于 2024-11-19 15:25:06 字数 325 浏览 9 评论 0原文

我想将来自 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 技术交流群。

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

发布评论

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

评论(1

羁客 2024-11-26 15:25:07

类似但不相同的问题有助于:
Java - 是否可以将 JLayeredPane 放入 JScrollPane 中? 如何在 JTable 单元格中显示动画使用 JScrollPane 和JLayeredPane

要正确执行此操作,需要考虑三个单独的问题:大小调整、标题和 UI 更改。

调整大小

要正确滚动和绘制,JScrollPane 需要知道其内部组件的大小和首选大小,在本例中为 JLayeredPane。但是您希望由表格设置大小,因为其他组件将浮动在表格顶部。在这种情况下,最简单的方法是将 JLayeredPane 将与大小相关的属性委托给 JTable,如下所示。

final JTable table = new JTable();
JLayeredPane layers = new JLayeredPane() {
  @Override
  public Dimension getPreferredSize() {
    return table.getPreferredSize();
  }

  @Override
  public void setSize(int width, int height) {
    super.setSize(width, height);
    table.setSize(width, height);
  }

  @Override
  public void setSize(Dimension d) {
    super.setSize(d);
    table.setSize(d);
  }
};
// NB you must use new Integer() - the int version is a different method
layers.add(label, new Integer(JLayeredPane.PALETTE_LAYER), 0);
JScrollPane scrolling = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolling.setViewportView(layers);

如果您不希望 JTable 决定 JLayeredPane 的大小,则需要以其他方式确定它,表格的大小也是如此。两者都需要显式调用 setPreferredSize()setSize()

标题

由于 JTable 不再是视口,因此您需要自己链接标题。以下代码将起作用:

scrolling.setColumnHeaderView(table.getTableHeader());
scrolling.setRowHeaderView(rowHeader);

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 the JLayeredPane. But you want the size to be set by the table, as other Components will be floating on top of the table. In this case the easiest way is to make the JLayeredPane delegate size related properties to the JTable as follows.

final JTable table = new JTable();
JLayeredPane layers = new JLayeredPane() {
  @Override
  public Dimension getPreferredSize() {
    return table.getPreferredSize();
  }

  @Override
  public void setSize(int width, int height) {
    super.setSize(width, height);
    table.setSize(width, height);
  }

  @Override
  public void setSize(Dimension d) {
    super.setSize(d);
    table.setSize(d);
  }
};
// NB you must use new Integer() - the int version is a different method
layers.add(label, new Integer(JLayeredPane.PALETTE_LAYER), 0);
JScrollPane scrolling = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                                        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolling.setViewportView(layers);

If you didn't want the JTable to be the thing which determines the size of the JLayeredPane then it needs to be determined in some other way, and so does the table's size. Both will need setPreferredSize() and setSize() 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:

scrolling.setColumnHeaderView(table.getTableHeader());
scrolling.setRowHeaderView(rowHeader);

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.

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