第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

添加數(shù)據(jù)后 JTable 不會更新

添加數(shù)據(jù)后 JTable 不會更新

元芳怎么了 2023-01-05 16:51:58
JTable從 SQL Server 2008 數(shù)據(jù)庫添加行后不更新。在 jaybird 2.2.5 上使用 Netbeans 8.2 我嘗試將 移動到不同的地方,setModel()我已經(jīng)使用過但沒有任何反應(yīng)。fireTableDataChanged()repaint()String[] colNames = {"#", "Name", "Surname", "Grades", "House", "Prefect"};DefaultTableModel model = new DefaultTableModel(colNames, 0);Validation.newStudent(db, stringName, stringSurname); //Creates a new row with a name and a surnameint maxval = Integer.parseInt(Validation.getNumberOfStudents(db)); //Counts the amount of students in the tableString[][] rowData = new String[maxval][50];model.setRowCount(0); //to reset the rowsfor (int i = 0; i < maxval; i++){    String[] row = new String[maxval];    for (int j = 0; j < maxval; j++){        rowData[i][0] = Integer.toString(i+1);        rowData[i][1] = Validation.listStudentNames(db).get(i);        rowData[i][2] = Validation.listStudentSurnames(db).get(i);        rowData[i][3] = Validation.studentsByGrade(db).get(i);        rowData[i][4] = Validation.studentsByHouse(db).get(i);        if (Validation.listPrefects(db).get(i) == null){ //checks if a student is a prefect            rowData[i][5] = "No";        }else{            rowData[i][5] = "Yes";        }        row[j] = rowData[i][j];    }    model.addRow(row); }teacherContentTable.setModel(model);該表應(yīng)該與新學(xué)生一起更新,但它保留了舊表模型。
查看完整描述

1 回答

?
森欄

TA貢獻1810條經(jīng)驗 獲得超5個贊

沒有回答您的問題,但是您重新填充模型的邏輯過于復(fù)雜。


無需創(chuàng)建二維數(shù)組,因為您一次將一行數(shù)據(jù)添加到模型中。


此外,只需使用 Vector,因為 DefaultTableModel 無論如何都會將行數(shù)組轉(zhuǎn)換為 Vector。


所以你的代碼的基本結(jié)構(gòu)更像是:


String[] colNames = {"#", "Name", "Surname", "Grades", "House", "Prefect"};

DefaultTableModel model = new DefaultTableModel(colNames, 0);

Validation.newStudent(db, stringName, stringSurname); 

int maxval = Integer.parseInt(Validation.getNumberOfStudents(db)); 


for (int i = 0; i < maxval; i++)

{

    Vector<String> row = new Vector<String>();


    for (int j = 0; j < maxval; j++){


        row.add( Integer.toString(i+1) );

        row.add( Validation.listStudentNames(db).get(i) );

        row.add( Validation.listStudentSurnames(db).get(i) );

        ...

    }


    System.out.println( row ); // make sure you actually add data to the model.

    model.addRow(row); 

}


teacherContentTable.setModel(model);

現(xiàn)在假設(shè)您實際將數(shù)據(jù)添加到模型中,表格將被更新,再次假設(shè)您對添加到框架中的 JTable 實例有正確的引用。


編輯:


上面的代碼與您所說的用新模型更新 JTable 的問題無關(guān)。


可以用一行代碼代替:


teacherContentTable.setModel( new DefaultTableModel(5, 5) );

您現(xiàn)在應(yīng)該看到一個包含 5 行和 5 列的空 JTable。在擔(dān)心從數(shù)據(jù)庫中獲取數(shù)據(jù)之前,您首先要讓它工作。


查看完整回答
反對 回復(fù) 2023-01-05
  • 1 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號