5

这是一个 try 块,用于过滤 table_job 以查找匹配关键字的行。但是,当表模型发生变化时,我很难获得正确的行索引。它总是选择第一行,即使过滤结果显示的行不是第一行。

我知道你可以用 做一些事情fireTableDataChanged(),但我不确定如何和在哪里做这件事,在trycatch块中或在setLabelText()将表格内容显示为的方法中. 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());正在拾取错误的行,而不是更新的选定行。我怎样才能考虑到这一点?

4

2 回答 2

9

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

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

String table_click0 = table_job.getModel().getValueAt(table_job.
                          convertRowIndexToModel(row), 0).toString());
于 2013-01-13T18:23:21.697 回答
0

快速修复:如何在过滤列表中获取实际行

int row=getjTable().getSelectedRow();
if (getjTable().getRowSorter()!=null) {
    row = getjTable().getRowSorter().convertRowIndexToModel(row);
}
于 2016-09-28T15:12:10.100 回答