public class HiwayTest6 { public static void main(String...args){
JFrame frame = new JFrame("Test");
Vector rowData = new Vector();
Vector column = new Vector();
for(int i=0;i<10;i++){
Vector vec = new Vector();
for(int j=0;j<10;j++){
vec.add("("+i+","+j+")");
}
rowData.add(vec);
}
for(int i=0;i<10;i++){
column.add(i);
}
DefaultTableModel model = new DefaultTableModel(rowData,column);
JTable table = new JTable(model);
model.addColumn("add1");
table.addColumn(new TableColumn(1));
System.out.println(model.getColumnCount()+"\t"+table.getColumnCount()+"\t"+table.getTableHeader().getColumnModel().getColumnCount()+"\t"+table.getColumnModel().getColumnCount());
frame.setContentPane(new JScrollPane(table));
frame.setBounds(new Rectangle(10,10,800,600));
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
the output:
11  12   12   12why ?

解决方案 »

  1.   

    jtable 调用 addColumn 只影响 到 TableColumnModel,TableModel 没有得到反馈。
      

  2.   

    to 1L:
    那如何让TableModel得到信息呢?another question:
    add these two lines:table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setPreferredSize(new Dimension(1200,400));I put JTable in a JScrollPane, then set PreferredSize to make sure HorizontalScrollBar to be shown.But now when I drag the horizontalBar, the tabelHeader doesn't update.How to solve it ? Thank you 
      

  3.   

    to 1L:
    I solve the tableHeader question:
    add this line of code:table.getTableHeader().setPreferredSize(new Dimension(1200,20));dear huntor:
      can you help me to solve the tableModel and tableColumnModel question?Thank you
      

  4.   

    JTable 的 addColumn removeColumn moveColumn 只是用来控制 TableModel中数据显示的,比如 不想显示某一列时,使用 removeColumn ,想显示时 使用 addColumn 在加回来。真的想多加一列数据时,使用 TableModel的addColumn方法,JTable会自动显示的。