How to insert a column at a specific position in JTable

MhdSyrwan picture MhdSyrwan · Jan 1, 2012 · Viewed 11.5k times · Source

I have a JTable with a predefined model

how to ask the model to insert a specific column in a specific position ?

so i want something like : DefaultTableModel.insertRow(int,Object[]) for columns

Answer

Robin picture Robin · Jan 1, 2012

Is it really necessary to add the column in your TableModel at a specific index ? You can more easily adjust the column order in the view (the JTable) as documented in the class javadoc of JTable

By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model.

This is achieved by using the JTable#moveColumn method.

Adding a column to your DefaultTableModel is done calling the DefaultTableModel#addColumn method