reload lại jtable khi click item jcombobox

phungquachvinh.c3mx

New Member
2/7/20
2
0
1
st
Chào mọi người.
Mình có vấn đề nhờ sự giúp đỡ của mọi người.
Mình viết một app load data từ mysql vào jtable, khi load toàn bộ data vào jtable thì mình làm được.
Giờ mình muốn click chọn 1 item trong jcombobox thì dữ liệu trong jtable sẽ reload lại theo item được chọn (dùng SELECT * FROM ... WHERE....).
Nhờ mọi người giúp mình ạ
 

Attachments

Joe

Thành viên VIP
21/1/13
2,962
1,307
113
@phungquachvinh.c3mx
Working with IDE to create a GUI app is dangerous for those who are newbies. The embedded and chained GUI components make the app more confusing and complex. The generated codes are hard to follow and difficult to amend or to correct in case of extension or correction. Further, IDE generates too many superfluous little classes and that slowdowns the performance and the response time. More classes = more confusing = bad maintenance.
My advice: try to develop little GUI apps with your own codes then you learn more and so that you could better see the problems in case of bugs.

Back to your codes:
Java:
        tbl_chiso.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
Too complicated, hard to correct. Reasons: the model is set with two intrinsic arrays and that makes the correction impossible. Correction:
Java:
public class Chiso extends AbstractTableModel  {
   public Chiso(...) {
      ...
   }
   ...
}
The new Chiso model is now clearer and easier to modify or to enhance. Now to your problems
Java:
    public void showTable(){
          for(ChiSo cs : chiso){
            model.addRow(new Object[]{cs.getStartDate(), cs.getEndDate(), cs.getDate(), cs.getMonth(), cs.getDay(),
                                        cs.getYear(), cs.getTime(), cs.getPh(), cs.getTemp(), cs.getIso()});
        }
    }
    private void cbb_thongsoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbb_thongsoActionPerformed
        //String item = String.valueOf(cbb_thongso.getSelectedItem());  //<---too complicated
        String item = (String) cbb_thongso.getSelectedItem();
        show.setText(item);
    
        try {
            List<ChiSo> chisoitem = chisoservice.getChiSoItem(item);
            if (chisoitem == null || chisoitem.size() == 0) { //<---- in case of NULL
                System.out.println("ChisoItem is empty or null with:"+item);
                return;
            }
            for (ChiSo cs : chisoitem){
                model.addRow(new Object[]{cs.getStartDate(), cs.getEndDate(), cs.getDate(), cs.getMonth(), cs.getDay(),
                                        cs.getYear(), cs.getTime(), cs.getPh(), cs.getTemp(), cs.getIso()});
            }
        } catch (SQLException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_cbb_thongsoActionPerformed
If you have troubles with the JTable YOU HAVE to verify the DB Access. Because I don't use SQL/MySQL so I cannot verify it for you. Sorry.
 
Sửa lần cuối:
  • Like
Reactions: phungquachvinh.c3mx