在JTable中实现搜索方法

时间:2014-06-10 08:37:10

标签: java swing 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);
}

有人可以查看代码并解释为什么在从方法private void buildJTable(ArrayList<SalesOrder> list)构建时不搜索我的JTable?它构建了所有相关内容,但它不会搜索。

谢谢。

1 个答案:

答案 0 :(得分:1)

您只是比较不兼容的值。尝试按以下方式更改搜索方法

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
    {

    }
  }
}