Implementing a search method in JTable

I’m trying to implement a search method in JTable, the search code is:

private void searchTable()
{
    String id = inputIdField.getText();

    for(int row = 0; row <= listAllTable.getRowCount() - 1; row++) 
    {
        System.out.println(row);
        if(id.equals(listAllTable.getValueAt(row, 0))) 
        {
            listAllTable.scrollRectToVisible(listAllTable.getCellRect(row, 0, true));
            listAllTable.setRowSelectionInterval(row, row);
            for (int i = 0; i <= listAllTable.getColumnCount() - 1; i++) 
            {

                listAllTable.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
            }
        }
        else
        {

        }
    }
}

The method works only when I add String and Object:

final String[] columnNames = {"Name", "Surname", "Age"};

final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
                        {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
                        {"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"}};

JTable listAllTable = new JTable(data, columnNames);

But when I fetch from DB, put it in the array and display it in the JTable the search method does not work, it get the rows perfectly, but it does not pass the if(id.equals(listAllTable.getValueAt(row, 0))) when the value exists, can somebody check the code and explain me why? Here is the code for building my JTable:

private void buildJTable(ArrayList<SalesOrder> list)
{
    DefaultTableModel model = new DefaultTableModel()
    {
        private static final long serialVersionUID = 1L;
        public boolean isCellEditable(int row, int column) 
        {
            return false;
        }
    };
    model.setColumnIdentifiers(new String[] {"ID", "Customer ID", "Employee ID", "Voucher ID", "Status", "Date", "Price"});
    model.setRowCount(list.size());
    int row = 0;
    for(SalesOrder so : list)
    {
        model.setValueAt(so.getId(), row, 0);
        model.setValueAt(so.getCustomerId().getId(), row, 1);
        model.setValueAt(so.getEmployeeId().getId(), row, 2);
        model.setValueAt(so.getVoucherId().getId(), row, 3);
        model.setValueAt(so.getStatus(), row, 4);
        model.setValueAt(so.getDate(), row, 5);
        model.setValueAt(so.getPrice(), row, 6);
        row++;
    }
    listAllTable.setRowSelectionAllowed(true);

    listAllTable.setModel(model);
}

Can somebody look at the code and explain me why it does not search my JTable when build from a method private void buildJTable(ArrayList<SalesOrder> list) ? It builds everything correclty but it does not search.

Thank you.

Answer

You are simply compare incompatible values. Try to change your search method as following

private void searchTable()
{
  String id = inputIdField.getText();

  for(int row = 0; row <= listAllTable.getRowCount() - 1; row++) 
  {
      System.out.println(row);
      String strVal = null == listAllTable.getValueAt(row, 0)? null : listAllTable.getValueAt(row, 0).toString();
      if(id.equals(strVal)) 
      {
        listAllTable.scrollRectToVisible(listAllTable.getCellRect(row, 0, true));
        listAllTable.setRowSelectionInterval(row, row);
        for (int i = 0; i <= listAllTable.getColumnCount() - 1; i++) 
        {

            listAllTable.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
        }
    }
    else
    {

    }
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *