简体   繁体   中英

Java Swing: Select specefic row of Jtable after filtering data

The task is to filter data in jTable and pass selected values. This issue is that after filtering the data in jTable, it outputs the old value, that were at that position before filtering.

Suppose I have a table having one column, values are 1,2,3,4,5,6. After filtering I got only one value in the jTable, let's say, 6. So, it should output 6, but the output is 1. as 1 is there in the table before filtering. In the below code, first function filters the table according to the text provided in the textfield. The second function output selected value. How to update the jTable, so it gives me output on the basis of filtered data not on the basis of original table. Thank You.

private void jTextField_searchRecordKeyReleased(java.awt.event.KeyEvent evt) {                                                    

        DefaultTableModel table=(DefaultTableModel)this.jTable_Search.getModel();
        String query=this.jTextField_searchRecord.getText();

        TableRowSorter<DefaultTableModel> tr=new TableRowSorter<DefaultTableModel>(table);
        jTable_Search.setRowSorter(tr);
        tr.setRowFilter(RowFilter.regexFilter("(?i)" + query));
    }                                                   

    private void jButton_LocateMouseClicked(java.awt.event.MouseEvent evt) {                                            
       int column = 0;
       int rows[]=this.jTable_Search.getSelectedRows();

       for(int i=0;i<rows.length;i++)
       {
           String value = jTable_Search.getModel().getValueAt(rows[i], column).toString();
           System.out.println(value);
       }       
    }             

Selection index in the table and in the model are two different thing. But you can convert one to another. JTable API has some conversion methods. In your case is the method convertRowIndexToModel is important. So your code should look like:

private void jButton_LocateMouseClicked(java.awt.event.MouseEvent evt) {                                            
   int column = 0;
   int rows[]=this.jTable_Search.getSelectedRows();

   for(int i=0;i<rows.length;i++)
   {
       int modelRow = jTable_Search.convertRowIndexToModel(rows[i]);
       String value = jTable_Search.getModel().getValueAt(modelRow, column).toString();
       System.out.println(value);
   }       
}   

Another possibility is to use the method getValueAt of your table.

private void jButton_LocateMouseClicked(java.awt.event.MouseEvent evt) {                                            
   int column = 0;
   int rows[]=this.jTable_Search.getSelectedRows();

   for(int i=0;i<rows.length;i++)
   {
       String value = jTable_Search.getValueAt(rows[i], column).toString();
       System.out.println(value);
   }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM