簡體   English   中英

如何在 java swing 中向 jtable 添加行值?

[英]how to add row values to jtable in java swing?

我有一個jcombo 框,其中有一些項目,如“教科書”、“大學書”、“歷史書”。我有動態ArrayList object 相應的書......當我點擊組合框項目“教科書”或“歷史書”時,它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在此使用了默認表 model 。但是當我使用默認表 model添加 3 或 4 行時,它會在此處附加前面的行。如果我在 for 循環中使用 removeRow(i),它只刪除 1 行或 2 行。 ..假設我有7行..它沒有刪除以前的所有行..我無法解決這個..請如果有人知道這個,請幫助...

您可能需要查看如何使用表格作為准備sscce的指南。 當您使用DefaultTableModel時,您需要展示如何構造傳遞給addRow()Object[]以及如何計算傳遞給removeRow()的索引。

如果我在 for 循環中使用 removeRow(i),它只刪除 1 行或 2 行...假設我有 7 行,它不會刪除以前的所有行

當您刪除多行時,您需要將該行從表的末尾刪除到 0:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM