0

So I'm trying to after searching a name click on the table and then edit it in other table, the problem is that I'm not getting the right ID but instead only getting the ID that is the first.

JTable

Search in action

ID wrong

Edit Code

int linha = this.jTable1.getSelectedRow(); int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(linha, 0)); Utilizador uti = UtilizadorJpaController.read(idUtilizador); CriarCliente updateCliente = new CriarCliente(uti); updateCliente.setVisible(true); 

Search Code

DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(model); jTable1.setRowSorter(tr); tr.setRowFilter(RowFilter.regexFilter(jTextField1.getText().trim(),1)) 

1 Answer 1

0

When you sort or filter a table, the data in the TableModel doesn't change. Only the view of the data changes.

If you want to get the data from the model, then you need to convert the "view row" to the "model row" by using the convertRowIndexToModel(...) method:

int linha = this.jTable1.getSelectedRow(); int modelRow = jTable1.convertRowIndexToModel(linha); int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(modelRow, 0)); //int idUtilizador = Integer.parseInt((String)(this.jTable1.getModel() ).getValueAt(linha, 0)); 

Also, why are you using Integer.parseInt(...)? If the data in the model is an integer value, then it should be stored as an Integer value, not a String value.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.