3

我已经像这样创建了我的 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 列。在成员方法之间找不到这样的功能,有可能吗?谢谢。

4

1 回答 1

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();
            }
        });
    }
}
于 2013-05-30T23:07:06.987 回答