0
I am using JPA to fill my table that is located in a Jframe
.
Through JPA, I make a selection in my database to find all names that have part of the name I wrote, for example, if I input "m", all people with name starting with "m" should appear in my list, if they change to "mi" only those that have the name that begins with these two letters appears, and if the entry is empty, appears all clients in the table.
Until then everything right, the problem and that I am not able to clear my table for the next search, it just replaces the lines and the rest is the result of the previous search, I would like to know how to delete each search.
package br.com.exemplo.telas;
import br.com.exemplo.conexao.JPAUtil;
import br.com.exemplo.entity.Cliente;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JOptionPane;
//biblioteca rs2xml.jar
/**
*
* @author costa
*/
public class TelaConsultar extends javax.swing.JInternalFrame {
int idtb;
EntityManager conexao;
/**
* Creates new form TelaConsulta
*/
public TelaConsultar() {
initComponents();
conexao = new JPAUtil().getEM();
pesquisar();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
tblClientes = new javax.swing.JTable();
txtCliPes = new javax.swing.JTextField();
txtConId = new javax.swing.JTextField();
btnRemove = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setClosable(true);
setIconifiable(true);
setMaximizable(true);
setTitle("Consulta");
setPreferredSize(new java.awt.Dimension(640, 480));
tblClientes.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
tblClientes.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tblClientesMouseClicked(evt);
}
});
jScrollPane2.setViewportView(tblClientes);
txtCliPes.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtCliPesKeyReleased(evt);
}
});
txtConId.setEnabled(false);
txtConId.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtConIdActionPerformed(evt);
}
});
btnRemove.setText("Excluir Cliente");
btnRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRemoveActionPerformed(evt);
}
});
jButton2.setText("Edita Cliente");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(txtCliPes, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(60, 60, 60)
.addComponent(txtConId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 594, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(21, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2)
.addGap(73, 73, 73)
.addComponent(btnRemove)
.addGap(221, 221, 221))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtCliPes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtConId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 31, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(94, 94, 94)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnRemove)
.addComponent(jButton2))
.addGap(67, 67, 67))
);
setBounds(0, 0, 641, 480);
}// </editor-fold>
// O evento abaixo e do tipo "enquanto vou pesquisando"
private void txtCliPesKeyReleased(java.awt.event.KeyEvent evt) {
pesquisar();
}
//Enveto que serar usado para setar os campos da tabela clicando com o mouse
private void tblClientesMouseClicked(java.awt.event.MouseEvent evt) {
int setar = tblClientes.getSelectedRow();
System.out.println(tblClientes.getModel().getValueAt(setar, 0).toString());
idtb = Integer.parseInt(tblClientes.getModel().getValueAt(setar, 0).toString());
//Abre o form TelaCliente
/*try {
int setar = tblClientes.getSelectedRow();
String id = tblClientes.getModel().getValueAt(setar, 0).toString();
TelaAterar tela = new TelaAterar();
tela.setVisible(true);
tela.setarCampos(id);
TelaPrincipal desc = new TelaPrincipal();
desc.desktop.add(tela);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}*/
}
private void txtConIdActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {
int sair =JOptionPane.
showConfirmDialog(null, "Tem certeza que deseja apagar?", "Atenção",
JOptionPane.YES_NO_OPTION);
if (sair == JOptionPane.YES_OPTION) {
deletar();
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
TelaAterar tele = new TelaAterar();
TelaPrincipal tela = new TelaPrincipal();
tele.setVisible(true);
tela.desktop.add(tele);
}
// Variables declaration - do not modify
private javax.swing.JButton btnRemove;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable tblClientes;
private javax.swing.JTextField txtCliPes;
private javax.swing.JTextField txtConId;
// End of variables declaration
//Metodo para setar os campos do formulario com o conteudo da tabela
private void pesquisar() {
conexao.getTransaction().begin();
//limpaTabela();
String jpql = ("select c from Cliente c where c.nome like :cNome");
Query query = conexao.createQuery(jpql);
query.setParameter("cNome", txtCliPes.getText() + "%");
List<Cliente> cliente = query.getResultList();
int i = 0;
for (Cliente cli : cliente) {
tblClientes.setValueAt(cli.getId(), i, 0);
i++;
}
conexao.getTransaction().commit();
//Usar a Biblioteca rs2xlm.jar para preencher a tabela
// tblClientes.setModel(DbUtils.resultSetToTableModel(rs));
}
private void deletar() {
conexao.getTransaction().begin();
Cliente cli =conexao.find(Cliente.class, idtb);
conexao.remove(cli);
conexao.getTransaction().commit();
}
}
I’m pulling the table straight from Jframe, and that’s why I’m not using Deafaulttablemodel
– Miguel
@Miguelcosta then you created a Tablemodel of your own?
– user28595
Yes, I basically created a Jframe and pulled the Jtable from the Palette along with the text and button fields as you can see here. 

 private javax.swing.JButton btnRemove;
 private javax.swing.JButton jButton2;
 private javax.swing.JScrollPane jScrollPane2;
 private javax.swing.JTable tblClientes;
 private javax.swing.JTextField txtCliPes; private javax.swing.Jtextfield txtConId;
– Miguel
@Miguelcosta if you have created a Tablemodel of your own, then you should implement this removal on it. In the case of Default, it already has the feature ready so that you can make use without having to worry about implementation. In case of customizing, ai the responsibility of the resource is of the programmer.
– user28595
Yes, but this implementation that I’m not quite sure about, so I came here to find out some way that I haven’t thought about implementing
– Miguel
@Miguelcost puts your Tablemodel code in the question. Or try simply adding the snippet of the answer to see if it works.
– user28595
I just posted the class
– Miguel
@Miguelcost by the code posted, you are using the defaultTablelemodel yes, the solution of the answer will serve. Just put at the beginning of the search method.
– user28595
I already tried the above code, gave this error: Exception in thread "AWT-Eventqueue-0" java.lang.Arrayindexoutofboundsexception: 0 >= 0
– Miguel
@Miguelcosta if live want to clean and continue using the same tablemodel, you can not add new values using
setValueAt
, and yesaddRow
.– user28595
Now be running normally, thank you very much
– Miguel
@Miguelcosta how good it worked. I did an update, see if you did it this way, is the one I tried to explain in the previous comment.
– user28595
@Michael any reason for having undone the accepted? The answer had not answered him?
– user28595