如何从JTextField连续添加数据到JTable
How to add continuously data from JTextField to JTable
我想从 JTextFields
连续添加数据到 Jtable
。
当我单击添加按钮时,必须将 JTextFields 中的文本插入到 Jtable.
当我点击添加按钮时,这段代码只生成一行。
我希望将行添加到之前插入的行中。
public void actionPerformed(ActionEvent arg0) {
DefaultTableModel model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
我需要向 table 添加事件处理程序吗?谢谢你。请帮我完成作业。
移动这部分:
DefaultTableModel model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
到您的构造函数并将模型定义为实例成员。不要为每次按钮点击创建 table 模型。以下部分足够 actionPerformed
.
public void actionPerformed(ActionEvent arg0) {
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
编辑:
如果你分享你的完整代码,我可以告诉你把上面的部分放在哪里。但现在,下面的示例代码可以指导您。
public class TableClass {
DefaultTableModel model;
public TableClass() {
model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
JButton addButton = JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
})
}
}
我想从 JTextFields
连续添加数据到 Jtable
。
当我单击添加按钮时,必须将 JTextFields 中的文本插入到 Jtable.
当我点击添加按钮时,这段代码只生成一行。 我希望将行添加到之前插入的行中。
public void actionPerformed(ActionEvent arg0) {
DefaultTableModel model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
我需要向 table 添加事件处理程序吗?谢谢你。请帮我完成作业。
移动这部分:
DefaultTableModel model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
到您的构造函数并将模型定义为实例成员。不要为每次按钮点击创建 table 模型。以下部分足够 actionPerformed
.
public void actionPerformed(ActionEvent arg0) {
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
编辑:
如果你分享你的完整代码,我可以告诉你把上面的部分放在哪里。但现在,下面的示例代码可以指导您。
public class TableClass {
DefaultTableModel model;
public TableClass() {
model = new DefaultTableModel();
table_1.setModel(model);
model.addColumn("Product Name");
model.addColumn("Product Price");
model.addColumn("Quantity");
JButton addButton = JButton("Add");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = jFrame_pName.getText().trim();
String price = jFrame_pPrice.getText().trim();
String quantity = jFrame_quantity.getText().trim();
String st[] = {name, price, quantity};
model.addRow(st);
}
})
}
}