简体   繁体   中英

how to add row values to jtable in java swing?

I have a jcombo box which has some items like "schoolbooks","collegebooks","historybooks" .and i have dynamic ArrayList object of corresponding books ... When i click the combo box item "schoolbooks" or "historybooks",it should display the contents in JTable from the arraylist .while every action performing,the JTable has to display the contents of the corresponding item of 'schoolbooks' or 'historybooks'.it should not append new rows when every action is performing ...i have used default table model in this.but when i add 3 or 4 rows using default table model ,its appending the row with previous here.. if i use removeRow(i) in for loop ,its removing 1 row or 2 rows only... Its not removing previous all rows suppose if i have 7 rows ..i am not able to solve this..please if anyone know this,please help...

You might want to review How to Use Tables as a guide to preparing your sscce . As you are using DefaultTableModel , you'll need to show how you construct the Object[] passed to addRow() and how you calculate the index passed to removeRow() .

if i use removeRow(i) in for loop,its removing 1 row or 2 rows only...Its not removing previous all rows suppose if i have 7 rows

When you remove multiple rows you need to remove the row from the end of the table down to 0:

for (int i = table.getRowCount() - 1; i > 0; i--)
{
    // add logic here
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM