具有嵌套标头的JTable

时间:2013-06-17 23:57:07

标签: java swing header jtable

我正在尝试在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);

1 个答案:

答案 0 :(得分: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);
    }
}