0

i have JTable with one column as default. my program can add columns with spesific value but when i try to add data in spesific row and column, an entire row filled by data. i just want to fill data to spesific row and column. its looks like this code still read number of column as default. i dont know how to solve this. thanks

for (int i = 0; i < NumberOfColumn.length; i++) {
        TableColumn tbl = new TableColumn();
        tbl.setHeaderValue(i);
        table1.getColumnModel().addColumn(tbl);

    } 
table1.setValueAt(2014, 0, 14);

for example i want to fill data '2014' at row 0 column 14, but when i run this code, all column filled with '2014'

1 Answer 1

2

When you manually create a TableColumn you need to specify which column in the TableModel to get the data from, otherwise they all default to column 0.

 TableColumn tbl = new TableColumn(i);
4
  • but i got an error. thanks before. Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    – user1004
    Commented May 24, 2014 at 6:24
  • Did you actually add any rows to the TableModel? You can't just set the value of a cell unless you have added data to the model. Why are you manually adding column when you can just use the DefaultTableModel to specify the number of columns and rows. Post your SSCCE that demonstrates the problem if you need more help.
    – camickr
    Commented May 24, 2014 at 14:31
  • i add manually column because i want to change number of column according to user's input when running time. but i'll try use DefaultTableModel and add column manually if user's input bigger than DefaultTableModel. thanks
    – user1004
    Commented May 25, 2014 at 5:13
  • The DefaultTableModel has an addColumn(...) method.
    – camickr
    Commented May 25, 2014 at 17:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.