简体   繁体   English

过滤后JTable正确的行号

[英]JTable correct row number after filtering

Here is a try block which serves the purpose of filtering through table_job to find rows which match keyword. 这是一个try块,用于过滤table_job以查找匹配关键字的行。 However, when the table model changes, I am struggling to obtain the correct row index. 但是,当表模型更改时,我正在努力获取正确的行索引。 It always picks the the first row, even though the filtered result shows a row which is not first. 它总是选择第一行,即使过滤后的结果显示的行不是第一行。

I understand you can do something with fireTableDataChanged() , but I am not sure HOW and WHERE to do this, in the try and catch block OR in the setLabelText() method which displays the content of the table as . JLabel 我知道你可以用fireTableDataChanged()做一些事情,但是我不确定HOW和WHERE这样做,在trycatch块中或在setLabelText()方法中显示表的内容为. JLabel . JLabel

try 
{
    sql = "SELECT Job.jobID as 'Job ID', Employer.name as'Company', Job.title as 'Role', Job.description as  'Description', Job.type as 'Type', Job.benefits as 'Benefits', Job.closing as 'Closing Date' FROM Job INNER JOIN Employer ON Job.employerID=Employer.employerID ORDER BY Employer.name";
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    TableModel model = DbUtils.resultSetToTableModel(rs);
    table_job.setModel(model);
    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table_job.setRowSorter(sorter);               
    searchJob.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent e) 
        {
            String text = keyword.getText(); 
            if (text.length() == 0) 
            {
                sorter.setRowFilter(null);
            } 
            else 
            {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }             
    });
}
catch (Exception e) 
{
    e.printStackTrace();                
}

private void setLabelText() 
{
    try 
    {
        String table_click0 = (table_job.getModel().getValueAt(
                row, 0).toString());
        String sqlSt = "SELECT Employer.name, * FROM Job INNER JOIN Employer ON Job.employerID = Employer.employerID WHERE jobID='"+table_click0+"' ";
        //rest of code to Label text...
    }

The String table_click0 = (table_job.getModel().getValueAt(row, 0).toString()); String table_click0 = (table_job.getModel().getValueAt(row, 0).toString()); is picking up the wrong row, not the updated selected row. 正在拾取错误的行,而不是更新的选定行。 How can I take this into account? 我怎么能考虑到这一点?

You probably need to convert your "row" value to a model index value (if your row value is retrieved from a "view" point of view), using convertRowIndexToModel . 您可能需要使用convertRowIndexToModel将“行”值转换为模型索引值(如果从“视图”的角度检索row值)。 So just replace 所以只需更换

String table_click0 = (table_job.getModel().getValueAt(row, 0).toString());

with

String table_click0 = table_job.getModel().getValueAt(table_job.
                          convertRowIndexToModel(row), 0).toString());

Quick fix: how to get the actual row in a filtered list 快速修复:如何获取已过滤列表中的实际行

int row=getjTable().getSelectedRow();
if (getjTable().getRowSorter()!=null) {
    row = getjTable().getRowSorter().convertRowIndexToModel(row);
}

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

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