Hi, people, I used the following code to create my JTable object:
MyTable class was defined below:DefaultTableModel tableModel = new DefaultTableModel(); String columnNames[] = {"A", "B", "C", "D", "E"}; DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); for (int i = 0; i < 5; i++) { TableColumn column = new TableColumn(); column.setHeaderValue(columnNames[i]); columnModel.addColumn(column); tableModel.addColumn(column); } tableModel.addRow(new Object[5]);//for the first blank row MyTable table = new MyTable(tableModel, columnModel);
then, I input "a1" to the cell(0, 0), did the query and got the resultset: a1, b1, c1, d1, e1(for example) and set the values to the table:public class MyTable extends JTable { public MyTable(TableModel dm, TableColumnModel cm) { super(dm, cm); } @Override public void changeSelection(int row, int column, boolean toggle, boolean extend) { super.changeSelection(row, column, toggle, extend); if (editCellAt(row, column)) getEditorComponent().requestFocusInWindow(); } @Override /*only the first column is editable, the other columns' values are determined by the corresponding first column using a query from dababase */ public boolean isCellEditable(int row, int column) { if (column == 0) return true; else return false; } }
finally, I got a table like this:table.setValueAt(b1, 0, 1); table.setValueAt(c1, 0, 2); table.setValueAt(d1, 0, 3); table.setValueAt(e1, 0, 4);all the cells of this row have the same value e1, why comes out such weird thing?
A B C D E e1 e1 e1 e1 e1
Any ideas or suggestions are welcomed, thanks in advance!