Good afternoon. I am trying to create a JTable that will rearrange its rows when a column is relocated. I've adapted the original project to a simplified version that will allow me to post it all at once. There are a couple points I'd like to make before the actual code:

1.) I've implemented a getValueAt(int, int) method in the AbstractTableModel interface implementation.

2.) I've added a column listener to detect when a column move starts, and I set a flag (columnDragging) to indicate a column is being moved.

3.) I've added a mouse listener to the table header to detect when the mouse is released. If the flag is set, it means a column was being dragged when the mouse was released. In this way, I can respond to the column being moved after the user releases the mouse.

When the column order changes, the row order does change, but the values returned by the getValueAt function are returned as if the columns weren't rearranged. I think the issue is in the arguments JTable passes to that getValueAt function, which has something to do with convertColumnIndexToModel.

I would really appreciate some help on this. Thanks for your time!

Java Code:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class ExpTable extends JPanel
	{
	private JTable table;
	boolean columnDragging;
	boolean viewStable;
	public ExpTable()
		{
		super(new GridLayout(1,0));
		final ExpTableModel myModel = new ExpTableModel();
		table = new JTable(myModel);
		columnDragging = false;
		viewStable = false;
		table.getTableHeader().addMouseListener(new MouseAdapter()
		{
		@Override
		public void mouseReleased(MouseEvent e)
			{
			if(columnDragging)
				{
				System.out.println("Drag Completed");
				Vector<Integer> newColumns = new Vector<Integer>();
				for(int i = 1; i < table.getColumnCount(); i++)
					{
					newColumns.add(new Integer(table.getColumnName(i)));
					}
				myModel.repopulateTable(newColumns);
				myModel.fireTableDataChanged();
				table.setModel(myModel);
				PrintColumnNames();
				myModel.PrintTableData();
				}
			}
		});
		table.getColumnModel().addColumnModelListener(new TableColumnModelListener()
		{
			@Override
			public void columnAdded(TableColumnModelEvent e)
				{}

			@Override
			public void columnRemoved(TableColumnModelEvent e)
				{}

			@Override
			public void columnMoved(TableColumnModelEvent e)
				{
				columnDragging = true;
				}

			@Override
			public void columnMarginChanged(ChangeEvent e)
				{}

			@Override
			public void columnSelectionChanged(ListSelectionEvent e)
				{}
			
		});
		table.setPreferredScrollableViewportSize(new Dimension(500,300));
		table.setFillsViewportHeight(true);
		JScrollPane scrollPane = new JScrollPane(table);
		add(scrollPane);
		}
	
	private static void createAndShowGUI()
		{
		JFrame frame = new JFrame("Table Experiment");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ExpTable newTable = new ExpTable();
		newTable.setOpaque(true);
		frame.setContentPane(newTable);
		frame.pack();
		frame.setVisible(true);
		}
	
	public void PrintColumnNames()
		{
		System.out.print("Column Names: ");
		for(int i = 0; i < table.getColumnCount(); i++)
			{
			String buf = table.getColumnName(i) + " ";
			System.out.print(buf);
			}
		System.out.println();
		System.out.print("Table Data Model Column Names: ");
		for(int i = 0; i < table.getColumnCount(); i++)
			{
			String buf = table.getModel().getColumnName(i) + " ";
			System.out.print(buf);
			}
		System.out.println();
		System.out.print("Table Column Model Column Names: ");
		for(int i = 0; i < table.getColumnModel().getColumnCount(); i++)
			{
			System.out.print(table.getColumnModel().getColumn(i).getHeaderValue() + " ");
			}
		System.out.println();
		}
	
	public static void main(String[] args)
		{
		javax.swing.SwingUtilities.invokeLater(new Runnable()
		{
		public void run()
			{
			createAndShowGUI();
			}
		});
		}
	}

@SuppressWarnings("serial") 
class ExpTableModel extends AbstractTableModel
	{
	private Vector<Integer> columnNames;
	private Vector<Vector<Integer>> rowHolder;
	private int squareSize = 5;
	
	public ExpTableModel()
		{
		columnNames = new Vector<Integer>();
		rowHolder = new Vector<Vector<Integer>>();
		for(int i = 1; i <= squareSize; i++)
			{
			columnNames.add(new Integer(i));
			}
		columnNames.add(0, new Integer(0));
		for(int i = 0; i < columnNames.size(); i++)
			{
			Vector<Integer> rowData = new Vector<Integer>();
			rowData.add(0, columnNames.get(i));
			for(int j = 1; j <= squareSize; j++)
				{
				rowData.add(columnNames.get(i)*columnNames.get(j));
				}
			rowHolder.add(rowData);
			}
		}
	@Override
	public int getRowCount()
		{
		return rowHolder.size();
		}
	@Override
	public int getColumnCount()
		{
		return columnNames.size();
		}
	@Override
	public String getColumnName(int column)
		{
		return columnNames.get(column).toString();
		}
	@Override
	public Integer getValueAt(int rowIndex, int columnIndex)
		{
		Vector<Integer> row = rowHolder.elementAt(rowIndex);
		System.out.println("Element at row " + rowIndex + " and column " + columnIndex + ": " + row.elementAt(columnIndex).toString());
		return row.elementAt(columnIndex);
		
		}
	
	public void repopulateTable(Vector<Integer> newColumnNames)
		{
		columnNames.removeAllElements();
		columnNames = new Vector<Integer>(newColumnNames);
		columnNames.add(0, new Integer(0));
		rowHolder.removeAllElements();
		for(int i = 1; i < columnNames.size(); i++)
			{
			Vector<Integer> rowData = new Vector<Integer>();
			rowData.add(0, columnNames.get(i));
			for(int j = 1; j <= squareSize; j++)
				{
				rowData.add(columnNames.get(i)*columnNames.get(j));
				}
			rowHolder.add(rowData);
			}
		}
	
	public void PrintTableData()
		{
		for(int i = 0; i < getRowCount(); i++)
			{
			System.out.print("Row " + i + ": ");
			for(int j = 0; j < rowHolder.get(i).size(); j++)
				{
				System.out.print(rowHolder.get(i).get(j) + " ");	
				}
			System.out.println();
			}
		}
	}