gpt4 book ai didi

java - 执行查询后刷新JTable

转载 作者:行者123 更新时间:2023-12-01 14:10:08 25 4
gpt4 key购买 nike

我正在开发我的 Java MySql 项目。我正在向厨房部的工作人员展示他们需要为客人准备的饭菜。当我的应用程序启动时,它会从数据库中获取未准备好的膳食并将其显示在 JTable 中。完成后,他们检查表中的“完成”字段,然后按确认按钮。现在我希望当他们单击订单按钮时​​刷新我的表格,并且该表格仅显示他们需要准备的饭菜。我对此没有任何问题,我只需执行查询,就可以从数据库中获取未准备好的饭菜。我的问题是我不知道如何刷新表。在代码中我写了注释,我认为 JTable 需要刷新。我正在使用 AbstractTableModel。

我的 JTable 图片:/image/TksPx.jpg

这是我的 TableModel 类:

public class KitchenTableModel extends AbstractTableModel {

private ArrayList<WrapperKitchen> hrana;

public KitchenTableModel(ArrayList<WrapperKitchen> hrana2) {
this.hrana = hrana2;

}

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 8;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return hrana.size();
}

public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:return "Order number";
case 1:return "Room";
case 2:return "Category";
case 3:return "Meal";
case 4:return "Quantity";
case 5:return "Note";
case 6:return "Order time";
case 7:return "Done";
}
return null;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
WrapperKitchen jelo = hrana.get(rowIndex);
switch (columnIndex) {
case 0:return jelo.getIdUslugaHrana();
case 1:return jelo.getBrojSobe();
case 2:return jelo.getNazivKategorija();
case 3:return jelo.getNazivHrane();
case 4:return jelo.getKolicina();
case 5:return jelo.getNapomena();
case 6:return jelo.getDatumVrijeme();
case 7:return jelo.getIzvrseno();
}
return null;
}

@Override
public Class<?> getColumnClass(int columnIndex) {
if (columnIndex == 7)
return Boolean.class;
return super.getColumnClass(columnIndex);
}

@Override
public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == 7);
}

@Override
public void setValueAt(Object inValue, int inRow, int inCol) {
if(inRow < 0 || inCol < 0 || inRow >= getRowCount() )
return;

WrapperKitchen jelo= hrana.get(inRow);
switch (inCol) {
case 0:jelo.setIdUslugaHrana((int)inValue);break;
case 1:jelo.setBrojSobe((int)inValue);break;
case 2:jelo.setNazivKategorija((String)inValue);break;
case 3:jelo.setNazivHrane((String)inValue);break;
case 4:jelo.setKolicina((int)inValue);break;
case 5:jelo.setNapomena((String)inValue);break;
case 6:jelo.setDatumVrijeme((Date)inValue);break;
case 7:jelo.setIzvrseno((boolean)inValue);break;
default: throw new RuntimeException("something bad happen incorrect column " + inCol);
}
fireTableCellUpdated(inRow, inCol);

}

}

这是我的 JButton ActionListener 的代码,带有注释行:

    ActionListener a1 = new ActionListener() {
public void actionPerformed(ActionEvent a) {
IzvrseneNarudzbe.clear();
boolean izvrseno;
int id;
for(int red=0;red<KuhinjaListaJela.size();red++){
Object obj = Tablica.getModel().getValueAt(red, 7);
izvrseno=(boolean)obj;
if(izvrseno==true)
{
Object obj2 = Tablica.getModel().getValueAt(red, 0);
id=(int)obj2;
IzvrseneNarudzbe.add(id);
}
}
izvrsiQuery();
//IN THIS LINE I NEED TO REFRESH MY JTABLE

}

void izvrsiQuery(){
for(int i=0;i<IzvrseneNarudzbe.size();i++){
String SqlQuery="UPDATE `room_service`.`usluga_hrana` SET `izvrseno` = '" + 1 +"' WHERE `usluga_hrana`.`id_usluga_hrana` ="+IzvrseneNarudzbe.get(i);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager
.getConnection("jdbc:mysql://"
+ "localhost:3306/room_service",
"root", "");
Statement Stat = (Statement) con.createStatement();
int Rez = Stat.executeUpdate(SqlQuery);

Stat.close();
} catch (Exception e2) {
System.out.println(e2);
}
}
}

};

最佳答案

在按钮的 ActionListener 中,您应该调用 setValueAt() 以使用查询结果更新您的 TableModel。该模型将 fireTableCellUpdated() 通知表自行更新。

关于java - 执行查询后刷新JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18579936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com