Java 如何在JTable中添加行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3549206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add row in JTable?
提问by oneat
Do you know how I can add a new row to a jTable
?
你知道如何向 a 添加新行jTable
吗?
采纳答案by Serplat
The TableModel
behind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use a DefaultTableModel
TableModel
JTable的后面处理表后面的所有数据。为了在表中添加和删除行,您需要使用DefaultTableModel
To create the table with this model:
要使用此模型创建表:
JTable table = new JTable(new DefaultTableModel(new Object[]{"Column1", "Column2"}));
To add a row:
添加一行:
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3"});
You can also remove rows with this method.
您还可以使用此方法删除行。
Full details on the DefaultTableModel can be found here
可以在此处找到有关 DefaultTableModel 的完整详细信息
回答by OMG Ponies
Use:
用:
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");
// Append a row
model.addRow(new Object[]{"v1", "v2"});
回答by user3452695
Use
用
DefaultTableModel model = (DefaultTableModel) MyJTable.getModel();
Vector row = new Vector();
row.add("Enter data to column 1");
row.add("Enter data to column 2");
row.add("Enter data to column 3");
model.addRow(row);
get the model with DefaultTableModel modelName = (DefaultTableModel) JTabelName.getModel();
获取模型 DefaultTableModel modelName = (DefaultTableModel) JTabelName.getModel();
Create a Vector with Vector vectorName = new Vector();
创建一个向量 Vector vectorName = new Vector();
add so many row.add
as comumns
添加row.add
尽可能多的comumns
add soon just add it with modelName.addRow(Vector name);
尽快添加只需添加它 modelName.addRow(Vector name);
回答by Muhammad Rehan Qadri
To add rowto JTable
, one of the ways is:
要将行添加到JTable
,其中一种方法是:
1) Create table using DefaultTableModel:
1) 使用 DefaultTableModel 创建表:
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Code");
model.addColumn("Name");
model.addColumn("Quantity");
model.addColumn("Unit Price");
model.addColumn("Price");
JTable table = new JTable(model);
2) To add row:
2)添加行:
DefaultTableModel model = (DefaultTableModel) table.getModel();
model.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 4","Column 5"});
回答by Joel Karunungan
For the sake of completeness, first make sure you have the correct import so you can use the addRow
function:
为了完整起见,首先确保您有正确的导入,以便您可以使用该addRow
功能:
import javax.swing.table.*;
Assuming your jTable is already created, you can proceed and create your own add row method which will accept the parameters that you need:
假设您的 jTable 已经创建,您可以继续并创建您自己的 add row 方法,该方法将接受您需要的参数:
public void yourAddRow(String str1, String str2, String str3){
DefaultTableModel yourModel = (DefaultTableModel) yourJTable.getModel();
yourModel.addRow(new Object[]{str1, str2, str3});
}