Search in JTable does not return as expected

-1

I have a bug that I do not understand. The idea is simple, I should type a value in a field and it returns me in the table, this happens, however when I click on the value found in the search it selects me the first value inside the table

import java.awt.Color;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;

import br.sp.mogi.imperiocongelados.model.Client;
import br.sp.mogi.imperiocongelados.model.tables.ModelTableClient;

public class Teste extends JFrame 
{
    public Teste() 
    {
        inicializarComponentes();
        inicializarEventos();
    }

    private void inicializarComponentes() 
    {
        setTitle("Teste");
        setSize(490, 600);
        setResizable(false);
        setLocationRelativeTo(null);
        setLayout(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // Search
        JTextField txtSearchForCode = new JTextField();
        txtSearchForCode.setToolTipText("Busca por codigo");
        txtSearchForCode.setLocation(10, 10);
        txtSearchForCode.setSize(50, 25);
        add(txtSearchForCode);

        // Table
        JTable table = new JTable();
        table.setBackground(Color.WHITE);

        JScrollPane scroll = new JScrollPane(table);
        scroll.setBorder(new LineBorder(Color.BLACK));
        scroll.setLocation(10, txtSearchForCode.getY() + txtSearchForCode.getHeight() + 1);
        scroll.setSize(470, 540);
        scroll.getViewport().setBackground(Color.WHITE);
        add(scroll);

    }

    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) 
            {
                JOptionPane.showMessageDialog(this, "Erro de busca por codigo", "Erro", JOptionPane.ERROR_MESSAGE);
            }
        }
    }

    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());
    }

    private void inicializarEventos() 
    {
        // Select row on table
            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();
                        }
                }
            });

            // Search
            txtSearchForCode.addKeyListener(new KeyAdapter() 
            {
                @Override
                public void keyReleased(KeyEvent e) 
                {
                    searchForCode();
                }
            });

    }
}
    
asked by anonymous 13.09.2017 / 04:03

1 answer

5

You did not provide a Minimum, Full, and Verifiable example , but I suspect the cause is table and template filter index conflicts.

When making a filter in a table, you need to convert the filter index to the equivalent of the model index in order to retrieve the filtered line correctly:

int rowSel = suaTable.getSelectedRow();//pega o indice da linha na tabela
int indexRowModel = suaTable.getRowSorter().convertRowIndexToModel(rowSel);//converte pro indice do model

More information you can read in the links I left in the question and learn another answer , or by providing a <

  •     
  • 13.09.2017 / 04:49