How to refresh JTable in Java

CSDN Q & A 2022-02-13 06:08:16 阅读数:906

refresh jtable java

DefaultTableModel defaultTableModel=new DefaultTableModel();

 refreshButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { defaultTableModel.setColumnIdentifiers(new Object[]{
"mdseID","mdseName","mdseKinds","mdseBarcode","specification","mdseUnit","mdseState","CurrentStorage","lowestStorage","unitPrice","sellPrice","produceFactory"}); String sql="select * from mdseinfo"; ResultSet rs = DBHelper.query(sql); try { while(rs.next()){ try { String mdseID=rs.getString("mdseID"); String mdseName=rs.getString("mdseName"); String mdseKinds=rs.getString("mdseKinds"); String mdseBarcode=rs.getString("mdseBarcode"); String specification=rs.getString("specification"); String mdseUnit=rs.getString("mdseUnit"); String mdseState=rs.getString("mdseState"); float CurrentStorage=rs.getFloat("CurrentStorage"); float lowestStorage=rs.getFloat("lowestStorage"); float unitPrice=rs.getFloat("unitPrice"); float sellPrice=rs.getFloat("sellPrice"); String produceFactory=rs.getString("produceFactory"); defaultTableModel.addRow(new Object[]{
mdseID,mdseName,mdseKinds,mdseBarcode,specification,mdseUnit,mdseState,CurrentStorage,lowestStorage,unitPrice,sellPrice,produceFactory}); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } });JTable tb=new JTable(defaultTableModel);

The above is the form of my interface , When I click the refresh button, it will display and this table , But my problem now is , How to delete the contents of the table refreshed before each refresh , Then come out the contents of the newly refreshed table ( For example, this table will appear after pressing refresh for the first time , Refresh again and a new modified table will appear below , I want to delete the first form before )




Take the answer :

call defaultTableModel.setRowCount(0);
You can clear the data , Then add new data


copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130608142525.html