Saturday, February 5, 2011

searchings rows from table

i want to select particular rows from the JTable which contains a particular string.. please help me for this..

  • Call JTable.getModel then just loop through using TableModel.getValueAt

  • Something like this will do the trick:

    void selectMatchingRows(JTable table, String regex) 
    {    
      for (int row = 0; row < table.getModel().getRowCount(); row++) 
      {
        for (int col = 0; col < table.getModel().getColumnCount(); col++) 
        {
          if (table.getModel().getValueAt(row, col).toString().matches(regex)) 
          {
            table.getSelectionModel().setSelectionInterval(row, row);
          }
        }
      }
    }
    

    Making sure the ListSelectionModel.selectionMode is MULTIPLE_INTERVAL_SELECTION.

    From Nick Holt

0 comments:

Post a Comment