0

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

我的 JTable 的图片:http: //i.imgur.com/mfO2ts9.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);
            }
        }
    }

};
4

1 回答 1

1

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

于 2013-09-03T10:11:43.560 回答