在JTable中实现一个搜索方法



我想在JTable中实现一个搜索方法,搜索代码是:

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
        {
        }
    }
}

该方法仅在我添加String和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);

但是当我从DB取回,把它放在数组中,并在JTable中显示它的搜索方法不工作,它得到的行完美,但它不通过if(id.equals(listAllTable.getValueAt(row, 0)))时,值存在,有人可以检查代码,并解释我为什么?下面是构建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);
}

有人可以看看代码,并解释为什么它不搜索我的JTable时,从一个方法private void buildJTable(ArrayList<SalesOrder> list)构建?它正确地构建一切,但它不搜索。

谢谢。

你只是比较不兼容的值。尝试更改搜索方法,如下所示

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
    {
    }
  }
}

相关内容

最新更新