JTable's setModel(..) problem

Here is my little problematic program:

import javax.swing.*;

import javax.swing.table.*;

publicclass MyTableextends JFrame{

publicstaticvoid main(String[] args){

MyTable frame =new MyTable();

int rows = 3, columns = 2;

DefaultTableModel model1 =new DefaultTableModel(rows, columns);

for (int r = 0; r < rows; r++)

for (int c = 0; c < columns; c++)

model1.setValueAt(new Integer(r + c), r, c);

DefaultTableModel model2 =new DefaultTableModel(rows + 1, columns + 1);

for (int r = 0; r < rows + 1; r++)

for (int c = 0; c < columns + 1; c++)

model2.setValueAt(new Integer(r * c), r, c);

JTable table =new JTable(model1);

table.setAutoCreateColumnsFromModel(false);

Object[] newColumnData1 ={10, 20, 30};

Object[] newColumnData2 ={100, 200, 300};

System.out.println(table.getRowCount() +" rows and " + table.getColumnCount() +" columns");

table.setModel(model2);

System.out.println(table.getRowCount() +" rows and " + table.getColumnCount() +" columns");

frame.add(table);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

}

The problem is that after model2 is set, the table still has 2 columns (while the new model has 3). So, is it possible to set a new model such that the table will be updated automatically ?

[2582 byte] By [leonarda] at [2007-11-26 20:01:31]
# 1
what happens if you remove this line?//table.setAutoCreateColumnsFromModel(false);
Michael_Dunna at 2007-7-9 23:00:06 > top of Java-index,Desktop,Core GUI APIs...
# 2
I'm so stupid......Thanks :)
leonarda at 2007-7-9 23:00:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

OK... Here is why I added this line:

import javax.swing.*;

import javax.swing.table.*;

public class MyTable extends JFrame {

public static void main(String[] args) {

MyTable frame = new MyTable();

int rows1 = 4, columns1 = 5;

DefaultTableModel model1 = new DefaultTableModel(rows1, columns1);

for (int r = 0; r < rows1; r++)

for (int c = 0; c < columns1; c++)

model1.setValueAt(new Integer(r + c), r, c);

int rows2 = 10, columns2 = 9;

DefaultTableModel model2 = new DefaultTableModel(rows2, columns2);

for (int r = 0; r < rows2; r++)

for (int c = 0; c < columns2; c++)

model2.setValueAt(new Integer(r * c), r, c);

JTable table = new JTable(model2);

table.setModel(model1);

System.out.println(table.getRowCount() + " rows and " + table.getColumnCount() + " columns");

Object[] newColumnData1 = {true, false, false, true};

Object[] newColumnData2 = {"a", "b", "c", "d"};

table.setAutoCreateColumnsFromModel(false); // MUST BE HERE

insertColumn(table, newColumnData1, 1);

insertColumn(table, newColumnData2, 3);

frame.add(table);

frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

private static void insertColumn(JTable table, Object[] values, int index) {

DefaultTableModel model = (DefaultTableModel)table.getModel();

TableColumn column = new TableColumn(model.getColumnCount());

table.addColumn(column);

model.addColumn(null, values);

table.moveColumn(table.getColumnCount() - 1, index);

}

}

If you remove the line table.setAutoCreateColumnsFromModel(false); // MUST BE HERE

the insertColumn(..) method will not work properly !

How can I sove this contradiction ?

leonarda at 2007-7-9 23:00:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

> So, is it possible to set a new model such that the table will be updated automatically ?

reset the setAutoCreateColumnsFromModel() to true before setting the model of the table.

> If you remove the line

> table.setAutoCreateColumnsFromModel(false); // MUST BE HERE

>the insertColumn(..) method will not work properly !

> How can I sove this contradiction ?

There is a problem with the way the two methods interact with one another.

a) the following works:

model.addColumn( ... );

table.moveColumn( ... );

b) adding and moving two columns doesn't work:

model.addColumn( ... );

table.moveColumn( ... );

model.addColumn( ... );

table.moveColumn( ... );

c) adding two columns, then moving two columns does work:

model.addColumn( ... );

model.addColumn( ... );

table.moveColumn( ... );

table.moveColumn( ... );

I don't know whats going on.

camickra at 2007-7-9 23:00:06 > top of Java-index,Desktop,Core GUI APIs...
# 5
Does it means that there is no "normal" way to work with setting models and insert columns to a table ?There must be some solution ......Help !!
leonarda at 2007-7-9 23:00:07 > top of Java-index,Desktop,Core GUI APIs...