Skip to content
Advertisement

Automatically filter JTable once QR is scanned

I am trying to make an attendance system with QR generator and scanner using zxing. QR part is working fine but I have one problem right now. When the QR code get scanned, I want it to automatically filter the table/database with the same serial number through textfield (I am using setText to pass the text on QR to textfield.

Code for Filter:

 try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
            String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + tfSearch.getText() + "%'";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();

            jtTable.setModel(DbUtils.resultSetToTableModel(rs));

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }  

So far, I have this code on a key released event. I also tried adding it to other event listener but it doesn’t work like I want it to be.

Advertisement

Answer

Netbeans 8.2 (IDE I am currently using) does not have any event for document listener so adding it on code customizer helps.

@Override  
    public void changedUpdate(DocumentEvent e) {
   search();
  }
  @Override
  public void removeUpdate(DocumentEvent e) {
    search();
  }
   @Override
  public void insertUpdate(DocumentEvent e) {
   search();
  } 
  
  public void search() {
     try {
            con = DriverManager.getConnection("jdbc:mysql://localhost/studentlogin", "root", "");
            String sql = "SELECT * FROM studentqrlogin WHERE " + "SN like '%" + jftfSearch.getText() + "%'";
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            // String sql = "SELECT No, Title, Author, Genre, Lexile, Points FROM LibrarySystemDatabase WHERE "
            //  + "No like '%" + txtSearch.getText() + "%'";

            jtTable.setModel(DbUtils.resultSetToTableModel(rs));
           // updateTable();

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }    
    }
});

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement