JTable多行标头

时间:2014-05-29 12:04:12

标签: java swing jtable

我面临的问题的SSSCE发布在

之下
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;


public class TableTest extends JFrame{

    private static final long serialVersionUID = 1L;
    private JPanel panel;
    private JPanel buttonPanel;
    private JTable table;
    private JTable headerTable;
    JButton cancelButton;
    private DefaultTableModel  tableModel;
    private DefaultTableModel  headerTableModel;
    String fileName = null;

    public TableTest()  {
        init();
    }

    public void init() {
        setTitle("Table");

        //Panel
        panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        this.getContentPane().add(panel);

        buttonPanel = new JPanel();

        //cancel button
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

        buttonPanel.add(cancelButton);

        String[] headerNames = {"", "Col 1", "Col 2", "Col 1", "Col 2"};
        String[] mainHeaderNames = {"Header 1", "Header2"};

        headerTableModel = new DefaultTableModel();

        for (int i = 0; i < mainHeaderNames.length; i++) {
            headerTableModel.addColumn(mainHeaderNames[i]);
        }

        //table model
        tableModel = new DefaultTableModel() {
            private static final long serialVersionUID = 1L;
            public boolean isCellEditable(int row, int column) {
                if(column == 0){
                    return true;
                } else {
                    return false;
                }
            }
            public Class<?> getColumnClass(int columnIndex) {
                switch (columnIndex) {
                    case 0:
                        return Boolean.class;
                }
                return null;
            }
        };

        //set default model to the table
        table = new JTable(tableModel);
        headerTable = new JTable(headerTableModel);

        // set table header
        for (int col = 0; col < headerNames.length; col++) {
            tableModel.addColumn(headerNames[col]);
        }

        //set table dimensions
        int height = 0;
        int width = 1000;
        if(tableModel.getRowCount() < 20) {
            height = tableModel.getRowCount()*16;
        } else {
            height = 350;
        }
        table.setPreferredScrollableViewportSize(new Dimension(width, height));

        //table header column width and height
        table.getTableHeader().setPreferredSize(new Dimension(100,28));
        headerTable.getTableHeader().setPreferredSize(new Dimension(100,28));

        //Set the width for column
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
        colModel.setColumnSelectionAllowed(false);
        colModel.getColumn(0).setPreferredWidth(5);
        colModel.getColumn(1).setPreferredWidth(300);
        colModel.getColumn(2).setPreferredWidth(200);
        colModel.getColumn(3).setPreferredWidth(300);
        colModel.getColumn(4).setPreferredWidth(200);

        //table setting
        table.setFillsViewportHeight(true);
        headerTable.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        //The header of column should be fixed.If set to true - columns can be re-arranged
        table.getTableHeader().setReorderingAllowed(false);
        headerTable.getTableHeader().setReorderingAllowed(false);

        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        JScrollPane headerScrollPane = new JScrollPane(headerTable);
        headerScrollPane.setPreferredSize(new Dimension(100,28));
        setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        //Add the scroll pane to this panel.
        GridBagConstraints c = new GridBagConstraints();

        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.BOTH;
        panel.add(headerScrollPane, c);

        c.anchor = GridBagConstraints.CENTER;
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1.0;
        c.weighty = 1.0;
        panel.add(scrollPane, c);

        c.fill = GridBagConstraints.NONE;
        c.gridx = 0;
        c.gridy = 2;
        c.weightx = 0;
        c.weighty = 0;
        panel.add(buttonPanel, c);

        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

                int option = JOptionPane.showConfirmDialog(panel, "Do you want to close this window?", " Confirm" , JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

                if (option == JOptionPane.YES_OPTION) {
                    dispose();
                } else {
                    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
                }
            }
        });

        //Display the window.
        if(tableModel.getRowCount() >= 0){
            pack();
            setAlwaysOnTop(true);
            setLocationRelativeTo(this);
            setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run(){
                new TableTest();
            }
        });
    }

}

我在这里使用了两个 JTable

GUI

我试着实现,如果我在标题1和标题2之间移动分割,下表中的相应分割线也应该被移动。

由于我使用两个 jTables ,我不知道如何实现这一点。或者有没有其他任何方式来解决这个问题。请帮助。

1 个答案:

答案 0 :(得分:2)

你的布局错了。尝试制作这样的布局:

enter image description here