简体   繁体   English

Java Swing:过滤数据后选择Jtable的特定行

[英]Java Swing: Select specefic row of Jtable after filtering data

The task is to filter data in jTable and pass selected values. 任务是过滤jTable中的数据并传递选定的值。 This issue is that after filtering the data in jTable, it outputs the old value, that were at that position before filtering. 问题是,在过滤jTable中的数据之后,它会输出旧值,该值位于过滤前的那个位置。

Suppose I have a table having one column, values are 1,2,3,4,5,6. 假设我有一个只有一列的表,值是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. 过滤后,jTable中只有一个值,即6。因此,它应该输出6,但输出为1。因为过滤前表中存在1。 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. 如何更新jTable,因此它根据过滤数据而不是原始表为我提供输出。 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. JTable API有一些转换方法。 In your case is the method convertRowIndexToModel is important. 在您的情况下, convertRowIndexToModel方法很重要。 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. 另一种可能性是使用表的getValueAt方法。

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);
   }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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