不能添加学生信息
在swing中Jtable我先是输入菜品编号或者菜品名称,然后点查询按钮,然后就会清空表,把我查询的信息显示出来,但是我这个是直接点查询,表数据就没了,没添加成功,该怎么办呢????
package view;
import dao.GoodsDao;
import model.Goods;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class GoodsSel extends JFrame implements ActionListener {
JFrame s;
DefaultTableModel model;
JTable table;
JScrollPane sp;
String[] heads;
JButton btnSel;
JComboBox<String> box;
JTextField textId,textName;
public GoodsSel(){
s = new JFrame("查询菜品");
s.setLayout(null);
box = new JComboBox<>();
JLabel labId = new JLabel("菜品编号");
s.add(labId);
labId.setBounds(60,30,65,45);
textId = new JTextField();
s.add(textId);
textId.setBounds(130,40,100,25);
JLabel labName = new JLabel("菜品名称:");
s.add(labName);
labName.setBounds(250,30,65,45);
textName = new JTextField();
s.add(textName);
textName.setBounds(320,40,100,25);
btnSel = new JButton("查询");
s.add(btnSel);
btnSel.setBounds(600,39,65,25);
//导入表数据
JPanel jpTable = new JPanel();
jpTable.setLayout(null);
jpTable.setBounds(40,80,700,160);
//蚀刻边框
jpTable.setBorder(BorderFactory.createEtchedBorder());
s.add(jpTable);
box.addItem("菜品编号");
box.addItem("菜品名称");
box.addItem("菜品价格");
box.addItem("菜品描述");
//设置表格列名
heads = new String[]{"菜品编号","菜品名称","菜品价格","菜品描述"};
GoodsDao dao = new GoodsDao();
List<Goods> goodsList = dao.findAll();
Object[][] arr=new Object[goodsList.size()][4];
for (int i=0;i<goodsList.size();i++){
Goods goods = goodsList.get(i);
arr[i][0]=goods.getd_id();
arr[i][1]=goods.getd_name();
arr[i][2]=goods.getd_price();
arr[i][3]=goods.getd_describe();
}
//实例化表格模型
model = new DefaultTableModel(arr,heads);
//实例化表格
table = new JTable(model);
//实例化滚动面板
sp = new JScrollPane(table);
sp.setBounds(0,0,700,160);
jpTable.add(sp);
s.add(jpTable);
btnSel.addActionListener(this);
s.setSize(780,320);
s.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
s.setVisible(true);
}
//清空表格
public void clearModel(){
while (model.getRowCount()>0){
model.removeRow(0);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(btnSel)){
GoodsDao dao = new GoodsDao();
List<Goods> list = new ArrayList<>();
Goods c = new Goods();
if (box.getSelectedItem().equals("菜品编号")){
c = dao.findByID(textId.getText());
System.out.println("菜品编号");
clearModel();
list.add(c);
}else if (box.getSelectedItem().equals("菜品名称")){
list = dao.findByName(textName.getText());
System.out.println("菜品名称");
clearModel();
}
}
}
public static void main(String[] args) {
new GoodsSel();
}
}