如何在JTable中更改列的行?

时间:2013-05-30 21:00:01

标签: java swing jtable row tablemodel

我已经创建了我的JTable:

        String[] columns = {"Country", "Continent", "Latitude", "Longitude"};
        String[][] data = {{"A","B","C","D"}};
        table = new JTable(data,columns);
        JScrollPane spTable = new JScrollPane(table);
        panel2.add(spTable);

现在我想改变表的外观,使行与列交换,这意味着表将有4行1列。在成员方法之间找不到这样的功能,有可能吗?感谢。

1 个答案:

答案 0 :(得分:3)

为此,我会考虑一个可以处理“切换”状态的自定义TableModel

从您的示例中,结果可能令人惊讶,但此代码向您显示了一般概念,并允许您使用按钮交换行和列。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.AbstractTableModel;

public class TestTable {

    private static class TogglingTableModel extends AbstractTableModel {

        private boolean toggled = false;
        private final Object[][] data;
        private final Object[] columnNames;

        public TogglingTableModel(Object[][] data, Object[] columnNames) {
            this.columnNames = columnNames;
            this.data = data;
            normalizeData();
        }

        private void normalizeData() {
            for (int i = 0; i < data.length; i++) {
                Object[] o = data[i];
                if (o.length < columnNames.length) {
                    data[i] = Arrays.copyOf(o, columnNames.length);
                }
            }
        }

        @Override
        public String getColumnName(int column) {
            if (toggled) {
                Object valueAt;
                if (column == 0) {
                    valueAt = columnNames[0];
                } else {
                    valueAt = data[column - 1][0];
                }
                return valueAt != null ? valueAt.toString() : null;
            } else {
                Object object = columnNames[column];
                return object != null ? object.toString() : null;
            }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public int getColumnCount() {
            if (toggled) {
                return data.length + 1;
            } else {
                return columnNames.length;
            }
        }

        @Override
        public int getRowCount() {
            if (toggled) {
                return columnNames.length - 1;
            } else {
                return data.length;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            if (toggled) {
                if (column == 0) {
                    return columnNames[row + 1];
                } else {
                    return data[column - 1][row + 1];
                }
            } else {
                return data[row][column];
            }
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            if (toggled) {
                if (column == 0) {
                    columnNames[row + 1] = aValue;
                } else {
                    data[column - 1][row + 1] = aValue;
                }
            } else {
                data[row][column] = aValue;
            }
        }

        public boolean isToggled() {
            return toggled;
        }

        public void toggle() {
            toggled = !toggled;
            fireTableStructureChanged();
        }
    }

    private TogglingTableModel tableModel;

    public TestTable() {
        JFrame frame = new JFrame(TestTable.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] columns = { "Country", "Continent", "Latitude", "Longitude" };
        String[][] data = { { "A" }, { "B" }, { "C" }, { "D" }, { "E" } };
        tableModel = new TogglingTableModel(data, columns);
        JTable table = new JTable(tableModel);
        JPanel buttonPanel = new JPanel();
        JButton togglingButton = new JButton("Toggle table");
        togglingButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                tableModel.toggle();
            }
        });
        buttonPanel.add(togglingButton);
        frame.add(new JScrollPane(table));
        frame.add(buttonPanel, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestTable fs = new TestTable();
            }
        });
    }
}