这是一个 try 块,用于过滤 table_job 以查找匹配关键字的行。但是,当表模型发生变化时,我很难获得正确的行索引。它总是选择第一行,即使过滤结果显示的行不是第一行。
我知道你可以用 做一些事情fireTableDataChanged()
,但我不确定如何和在哪里做这件事,在try
和catch
块中或在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());
正在拾取错误的行,而不是更新的选定行。我怎样才能考虑到这一点?