Hello everyone,
After I created a JTable with:
JTable MaTable =new JTable();
MaTable.setModel(new DefaultTableModel(new Object [N][1], new String [1]));

I wanted to add new columns to it, so I did this:
TableColumn Colonne = new TableColumn();
MaTable.addColumn(Colonne);

The operation did worked, even the new columns displayed correctly, but, when I set a value for one of the new cells, all the others (newly created) cells are modified!
It's like all new cells have the same index.
Did I miss something in my code that should correct the index? (if the problem is with the index).
I'm waiting for your precious help.

Recommended Answers

All 3 Replies

post the code you use for editing the single new cell.

I just used mytable.setValueAt(value,i,j) to edit new cells
nothing special

Here's the solution if you're interested.
Like I suspected, it was an Index problem. In my code listed above, I did not tell TableColumn which column in the model it refers to, so it defaults to 0 (or something common for all new cells). I should-ed type TableColumn colonne = new TableColumn(diagramme.getColumnCount());
So TableColumn refer every new column at the end of the table (that makes sens).
So, that was the major problem, but I did modified the code in order to work properly:
diagramme.setAutoCreateColumnsFromModel(false);
DefaultTableModel model = DefaultTableModel)diagramme.getModel();
TableColumn colonne = new TableColumn(diagramme.getColumnCount());
diagramme.addColumn(colonne);
model.addColumn(colonne);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.