0

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.

MariusJ
  • 83
  • 8
  • You should use the `addRow()` method for your model and let the model return the correct values from your `SalesOrder` class via by overriding `getValueAt()`. Take a look at [getValueAt](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#getValueAt%28int,%20int%29). There are also many examples online on how to implement an override for `getValueAt()` and `setValueAt()` inside a `DefaultTableModel` or `AbstractTableModel`. – David Yee Jun 10 '14 at 08:45

1 Answers1

1

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
    {

    }
  }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • @mKorbel I've only modified the code of the author of question to provide the required functionality. And RowFilter is not always the best solution, for example if you only want to highlight the matched rows (ok the example here is wrong for this case). – Sergiy Medvynskyy Jun 10 '14 at 10:41
  • @mKorbel I know how to do it, but it hasn't been asked in the question . – Sergiy Medvynskyy Jun 10 '14 at 10:57
  • asked, again hint (your answer is wrong add Renderer at runtime) this is job for [XxxTableModel](http://stackoverflow.com/questions/16814512/tablecellrenderer-and-how-to-refresh-cell-background-without-using-jtable-repain), resp. floating/variable params in Renderer – mKorbel Jun 10 '14 at 11:20