Returns the visible columns in the order that they appear in the model
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
public class Main {
public static void main(String[] argv) {
}
//
public TableColumn[] getColumnsInModel(JTable table) {
List result = new ArrayList();
for (Enumeration e = table.getColumnModel().getColumns(); e.hasMoreElements();) {
result.add((TableColumn) e.nextElement());
}
Collections.sort(result, new TableColumnComparator());
return (TableColumn[]) result.toArray(new TableColumn[result.size()]);
}
}
class TableColumnComparator implements Comparator {
public int compare(Object a, Object b) {
TableColumn c1 = (TableColumn) a;
TableColumn c2 = (TableColumn) b;
if (c1.getModelIndex() < c2.getModelIndex()) {
return -1;
} else if (c1.getModelIndex() == c2.getModelIndex()) {
return 0;
} else {
return 1;
}
}
}
Related examples in the same category