JTable过滤后正确的行号 [英] JTable correct row number after filtering

查看:140
本文介绍了JTable过滤后正确的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个try块,用于通过table_job进行过滤以查找与关键字匹配的行.但是,当表模型更改时,我正在努力获取正确的行索引.即使过滤后的结果显示的行不是第一行,它也始终会选择第一行.

Here is a try block which serves the purpose of filtering through table_job to find rows which match keyword. 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.

我知道您可以使用fireTableDataChanged()进行操作,但是我不确定如何在trycatch块中或在显示表内容的setLabelText()方法中执行此操作为. JLabel

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

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...
    }

String table_click0 = (table_job.getModel().getValueAt(row, 0).toString());正在选择错误的行,而不是更新的所选行.如何考虑到这一点?

The 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?

推荐答案

您可能需要将行"值转换为模型索引值(如果从视图"角度检索了row值) ,使用convertRowIndexToModel.所以只需替换

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. So just replace

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

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

这篇关于JTable过滤后正确的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆