Q:

How to change rows with columns in JTable?

I have created my JTable like this:
        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);

Now I want to change the look of the table in such a way that rows will be exchanged with columns, meaning that the table will have 4 rows and 1 column. Could not find such function between member methods, is it possible? Thanks.

A:

To do that, I would consider a custom TableModel which can handle a "toggled" state.
From your example, the result may be surprizing but this code shows you the general idea and allows you to swap rows and columns with a button.
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();
            }
        });
    }
}

