Estou com um bug que não estou entendendo. A ideia é simples, eu deveria digitar um valor em um campo e ele me retornar na tabela, isso acontece, porem quando clico em cima do valor encontrado na busca ele completa os campos com primeiro valor tabela
Método que faz a busca
private void searchForCode()
{
ModelTableClient tableClient = (ModelTableClient) table.getModel();
final TableRowSorter<ModelTableClient> sorter = new TableRowSorter<ModelTableClient>(tableClient);
table.setRowSorter(sorter);
String searchForCode = txtSearchForCode.getText();
if(searchForCode.length() == 0)
{
sorter.setRowFilter(null);
}
else
{
try
{
RowFilter<ModelTableClient, Object> rf = null;
try
{
rf = RowFilter.regexFilter(searchForCode, 0);
}
catch(java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
catch (Exception e)
{
System.out.println("Erro");
}
}
}
Método que completa os campos
public void completeFields()
{
txtName.setText(selectedClient.getName());
String dateText = dateFormat.format(selectedClient.getRegistrationDate());
ftRegistrationDate.setText(dateText);
ftDocument.setText(selectedClient.getDocument());
ftZipCode.setText(selectedClient.getZipCode());
txtState.setText(selectedClient.getState());
txtCity.setText(selectedClient.getCity());
txtNeighborhood.setText(selectedClient.getNeighborhood());
txtStreet.setText(selectedClient.getStreet());
ftNumber.setText(selectedClient.getNumber());
ftLandLine.setText(selectedClient.getLandline());
ftCellphone.setText(selectedClient.getCellPhone());
ftRoute.setText(selectedClient.getRoute());
}
Eventos
table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
int row = table.getSelectedRow();
if(row >= 0 && row < clientList.size())
{
selectedClient = clientList.get(row);
completeFields();
}
}
});
txtSearchForCode.addKeyListener(new KeyAdapter()
{
@Override
public void keyReleased(KeyEvent e)
{
searchForCode();
}
});
}