2

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

MhdSyrwan
  • 1,613
  • 3
  • 19
  • 26

4 Answers4

8

There is no insertColumn method like DefaultTableModel.insertRow() for inserting rows. In order to insert a column at a particular position, you must append the column using DefaultTable.addColumn() and then move the new column into the desired position.

    JTable table = new JTable(rows, cols);
    table.setAutoCreateColumnsFromModel(false);

    DefaultTableModel model = (DefaultTableModel)table.getModel();
    TableColumn col = new TableColumn(model.getColumnCount());

    col.setHeaderValue(headerLabel);
    table.addColumn(col);
    model.addColumn(headerLabel.toString(), values);
    table.moveColumn(table.getColumnCount()-1, vColIndex);
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
5

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

Robin
  • 36,233
  • 5
  • 47
  • 99
  • does re-ordering columns from Jtable affects the model ? – MhdSyrwan Jan 01 '12 at 15:51
  • 1
    No. This is even included in my answer (in the snippet copied from the javadoc). The order in the view is completely independent from the order in the model. That is the reason the `JTable` has all those convert index methods like e.g. [`convertColumnIndexToModel`](http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html#convertColumnIndexToModel(int)) – Robin Jan 01 '12 at 15:54
  • ok , this could make troubles with me , so how to addColumn without adding it to the model ? does JTable.addColumn affect the model ? – MhdSyrwan Jan 01 '12 at 16:01
  • Not. The JTable itself contains no data. All data is retrieved from the model. You could consider decorating an existing model to add an extra column without affecting your original model. And by using a decorator, you can add the column at any index you want – Robin Jan 01 '12 at 16:03
0

With DefaultTableModel:

At the end one has to call fireDataChanged. There is only an addColumn with several overloads. This is no hindrance as the order of display is independent. One may move a column to another position, and has to take care of view index != column index. To get the correct view index immediately after adding the column, one has to access the JTable and call moveColumn.

I found it at times easier to create a new TableModel and assign that. Or not use the DefaultTableModel.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • _At the end one has to call fireDataChanged_ No. a) adding a column is changing the _structure_, so the appropriate event to fire is structureChanged in a custom model implementation b) dtm.addColumn(..) already fires the correct event, no need to do anything – kleopatra Jan 07 '12 at 15:57
-1

this link may help http://www.roseindia.net/java/example/java/swing/InsertColumn.shtml

public void positionColumn(JTable table,int col_Index) {
 table.moveColumn(table.getColumnCount()-1, col_Index);
  }
paparoch
  • 450
  • 5
  • 10
  • @moderator: wrapping the exact method call already used in the more complete answer by yannis hristofakis into another method which doesn't add any value doesn't ... add any value ;-) Downvoted. – kleopatra Nov 22 '12 at 16:46