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);
Sign up to request clarification or add additional context in comments.

4 Comments

but i got an error. thanks before. Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
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.
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
The DefaultTableModel has an addColumn(...) method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.