0

我正在尝试在 Jtable 中执行嵌套标题,就像在http://www.esus.com/docs/GetQuestionPage.jsp?uid=1272中一样, 但这不起作用。

这是我尝试使用来自http://www.esus.com/docs/GetQuestionPage.jsp?uid=1272的类的代码:

DefaultTableModel tableModel = new DefaultTableModel();
tableModel.setDataVector(
        new Object[][]{},
        new String[]{
    "Klient", "Numer dokumentu", "E2", "Jedn.", "EUR", "H1", "E2", "Jedn.", "EUR", "H1"
});

JTable jTable1 = new javax.swing.JTable(tableModel) {
    @Override
    protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
    }
};

TableColumnModel cm = jTable1.getColumnModel();
ColumnGroup documentGroup = new ColumnGroup("Dokumenty");
ColumnGroup sendedGroup = new ColumnGroup("Wysłane");
ColumnGroup returnedGroup = new ColumnGroup("Zwracane");

documentGroup.add(cm.getColumn(0));
documentGroup.add(cm.getColumn(1));

sendedGroup.add(cm.getColumn(2));
sendedGroup.add(cm.getColumn(3));
sendedGroup.add(cm.getColumn(4));
sendedGroup.add(cm.getColumn(5));

returnedGroup.add(cm.getColumn(6));
returnedGroup.add(cm.getColumn(7));
returnedGroup.add(cm.getColumn(8));
returnedGroup.add(cm.getColumn(9));

GroupableTableHeader header = (GroupableTableHeader) jTable1.getTableHeader();
header.addColumnGroup(documentGroup);
header.addColumnGroup(sendedGroup);
header.addColumnGroup(returnedGroup);


jScrollPane1.setViewportView(jTable1);
4

1 回答 1

0

您的代码中似乎唯一缺少的是 JFrame 的设置,它将包含您的表格。我正在运行您的示例代码,如下所示:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class HeaderTableExample extends JFrame {
    public HeaderTableExample() {
        super("this is a title");
        DefaultTableModel tableModel = new DefaultTableModel();
        tableModel.setDataVector(
                new Object[][]{},
                new String[]{
                        "Klient", "Numer dokumentu", "E2", "Jedn.", "EUR", "H1", "E2", "Jedn.", "EUR", "H1"
                });

        JTable jTable1 = new javax.swing.JTable(tableModel) {
            @Override
            protected JTableHeader createDefaultTableHeader() {
                return new GroupableTableHeader(columnModel);
            }
        };

        TableColumnModel cm = jTable1.getColumnModel();
        ColumnGroup documentGroup = new ColumnGroup("Dokumenty");
        ColumnGroup sendedGroup = new ColumnGroup("Wysłane");
        ColumnGroup returnedGroup = new ColumnGroup("Zwracane");

        documentGroup.add(cm.getColumn(0));
        documentGroup.add(cm.getColumn(1));

        sendedGroup.add(cm.getColumn(2));
        sendedGroup.add(cm.getColumn(3));
        sendedGroup.add(cm.getColumn(4));
        sendedGroup.add(cm.getColumn(5));

        returnedGroup.add(cm.getColumn(6));
        returnedGroup.add(cm.getColumn(7));
        returnedGroup.add(cm.getColumn(8));
        returnedGroup.add(cm.getColumn(9));

        GroupableTableHeader header = (GroupableTableHeader) jTable1.getTableHeader();
        header.addColumnGroup(documentGroup);
        header.addColumnGroup(sendedGroup);
        header.addColumnGroup(returnedGroup);

        JScrollPane scroll = new JScrollPane(jTable1);
        getContentPane().add( scroll );
        setSize(400, 120);
        scroll.setViewportView(jTable1);
    }

    public static void main(String[] args) {
        HeaderTableExample frame = new HeaderTableExample();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.setVisible(true);
    }
}
于 2013-06-18T01:01:46.743 回答