JTable 列跨越

发布于 2024-07-05 14:05:24 字数 396 浏览 3 评论 0原文

我正在尝试制作一个具有可用列跨度的 JTable 。 具体来说,我希望将一个 JTable 嵌套在另一个 JTable 中,当用户单击查看嵌套表时,它应该展开以下推下面的行并填充空的空间。 这与您在 MS Access 中看到的类似,您可以在其中嵌套表,单击行上的展开按钮将显示嵌套表中的相应条目。

如果有人知道使用 JTable 执行列跨度的方法,您能给我指出正确的方向吗? 或者,如果您知道其他方法可以做到这一点,我愿意接受建议。 该应用程序是使用 Swing 构建的。 表中的元素(无论是高级元素还是低级元素)都必须在任何解决方案中都是可编辑的。 使用嵌套的 JTable 不会成为问题,任何其他解决方案也必须考虑到这一点。

I am trying to make a JTable that has column spans available. Specifically, I am looking to nest a JTable inside another JTable, and when the user clicks to view the nested table, it should expand to push down the rows below and fill the empty space. This is similar to what you see in MS Access where you can nest tables, and clicking the expand button on a row will show you the corresponding entries in the nested table.

If someone knows of a way to perform a column span with JTable, can you please point me in the right direction? Or if you know of an alternative way to do this, I am open to suggestions. The application is being built with Swing. Elements in the table, both high level and low level, have to be editable in any solution. Using nested JTables this won't be a problem, and any other solution would have to take this into consideration as well.

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

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

发布评论

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

评论(3

眼前雾蒙蒙 2024-07-12 14:05:25

您需要为主表编写自己的TableUI。 使用您自己的 TableModel 来保存附加数据(例如扩展行)也很有帮助。 但这是可选的。

我编写了一个 equals TableUI 来展开一行并显示文本编辑器。 在 TableUI 中,您需要使用 table.setRowHeight(height) 动态更改行高。 此外,还需要从 BaseTableUI 复制一些内容,因为您无法访问私有内容。

You need to write your own TableUI for the master table. It can also helpful to use your own TableModel to save additional data like if a row is expanded. But this is optional.

I write an equals TableUI that expand a row and show an text editor. In the TableUI you need to change the the row hight dynamically with table.setRowHeight(height). Also it is necessary to copy some stuff from the BaseTableUI because you can not access the private stuff.

救赎№ 2024-07-12 14:05:25

基于 Code-Guru 的代码:

/*
 *  (swing1.1beta3)
 * 
 * |-----------------------------------------------------|
 * |   1st  |      2nd    |      3rd         |
 * |-----------------------------------------------------|
 * |    |    |    |    |        |        |
 */
//package jp.gr.java_conf.tame.swing.examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

import jp.gr.java_conf.tame.swing.table.*;

/**
 * @version 1.0 11/09/98
 */
public class MultiWidthHeaderExample extends JFrame {

  MultiWidthHeaderExample() {
    super( "Multi-Width Header Example" );

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"a","b","c","d","e","f"},
      {"A","B","C","D","E","F"}},
    new Object[]{"1 st","","","","",""});

    JTable table = new JTable( dm ) {
      protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
      }
    };
    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g_2nd = new ColumnGroup("2 nd");
    g_2nd.add(cm.getColumn(1));
    g_2nd.add(cm.getColumn(2));
    ColumnGroup g_3rd = new ColumnGroup("3 rd");
    g_3rd.add(cm.getColumn(3));
    g_3rd.add(cm.getColumn(4));
    g_3rd.add(cm.getColumn(5));
    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g_2nd);
    header.addColumnGroup(g_3rd);
    JScrollPane scroll = new JScrollPane( table );
    getContentPane().add( scroll );
    setSize( 400, 100 );  
    header.revalidate(); 
  }

  public static void main(String[] args) {
    MultiWidthHeaderExample frame = new MultiWidthHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}

来源:http://www.codeguru.com/java /articles/125.shtml(自 2012 年以来不可用,现在参见 web archive)

其他资源:

Based on Code from Code-Guru:

/*
 *  (swing1.1beta3)
 * 
 * |-----------------------------------------------------|
 * |   1st  |      2nd    |      3rd         |
 * |-----------------------------------------------------|
 * |    |    |    |    |        |        |
 */
//package jp.gr.java_conf.tame.swing.examples;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

import jp.gr.java_conf.tame.swing.table.*;

/**
 * @version 1.0 11/09/98
 */
public class MultiWidthHeaderExample extends JFrame {

  MultiWidthHeaderExample() {
    super( "Multi-Width Header Example" );

    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"a","b","c","d","e","f"},
      {"A","B","C","D","E","F"}},
    new Object[]{"1 st","","","","",""});

    JTable table = new JTable( dm ) {
      protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
      }
    };
    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g_2nd = new ColumnGroup("2 nd");
    g_2nd.add(cm.getColumn(1));
    g_2nd.add(cm.getColumn(2));
    ColumnGroup g_3rd = new ColumnGroup("3 rd");
    g_3rd.add(cm.getColumn(3));
    g_3rd.add(cm.getColumn(4));
    g_3rd.add(cm.getColumn(5));
    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g_2nd);
    header.addColumnGroup(g_3rd);
    JScrollPane scroll = new JScrollPane( table );
    getContentPane().add( scroll );
    setSize( 400, 100 );  
    header.revalidate(); 
  }

  public static void main(String[] args) {
    MultiWidthHeaderExample frame = new MultiWidthHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}

Source: http://www.codeguru.com/java/articles/125.shtml (unavailable since 2012, see now in web archive)

Other ressources:

夢归不見 2024-07-12 14:05:24

作为正确方向的指针,请尝试 SwingWiki 上的这篇文章,其中解释了列的 TableUI 方法跨越得很好。 在此之前,我还尝试了一些替代方法,例如覆盖 TableCellRenderer 绘制方法,但没有取得太大成功。

As a pointer in the right direction, try this article at SwingWiki that explains the TableUI method of column spanning quite well. Before this, I also tried some alternatives such as overriding the TableCellRenderer paint methods without much success.

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