@BrunoFranco escreveu:
Preciso fazer uma busca que conforme o nome que eu digite ele me retorne o usuário na lista de usuários cadastrados como faço?
Minha classe entidade.
@Entity
public class Usuario implements Serializable{private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(length = 100) private String busca; @Column(length = 100) private String nomefuncionario; @Column(length = 100) private String login; @Column(length = 100) private String senhalogin; @Column(length = 100) private String confirmasenhalogin; @Column(length = 100) private String permissao; public Long getId() { return id; } public String getPermissao() { return permissao; } public void setPermissao(String permissao) { this.permissao = permissao; } public void setId(Long id) { this.id = id; } public String getNomefuncionario() { return nomefuncionario; } public void setNomefuncionario(String nomefuncionario) { this.nomefuncionario = nomefuncionario; } public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getSenhalogin() { return senhalogin; } public void setSenhalogin(String senhalogin) { this.senhalogin = senhalogin; } public String getConfirmasenhalogin() { return confirmasenhalogin; } public void setConfirmasenhalogin(String confirmasenhalogin) { this.confirmasenhalogin = confirmasenhalogin; }
}
Minha Tela
public class telacadastrousuario extends javax.swing.JInternalFrame {
Usuario usu = new Usuario();
private List listausuario = new ArrayList();
public telacadastrousuario() {
initComponents();
validatela("inicio");
montatabela();
}//Método com retorno limpa campos private void limpaCampos() { //Limpa os campos do formulário cnomefuncionario.setText(""); csenhalogin.setText(""); clogin.setText(""); cconfirmasenhalogin.setText("");
}
//Método com retorno valida campos private Boolean validaCampos() { String mensagem = ""; /*Vamos fazer comparações se o campo do formulário for vazio vai pedir enviar uma mensagem dizendo que o campo é obrigatorio*/ Boolean retorno = true; if (cnomefuncionario.getText().equals("")) { mensagem = mensagem + "O campo nome é obrigatório!\n"; retorno = false; } if (csenhalogin.getText().equals("")) { mensagem = mensagem + "O campo senha é obrigatório!\n"; retorno = false; } if (clogin.getText().equals("")) { mensagem = mensagem + "O campo login é obrigatório!\n"; retorno = false; } if (cconfirmasenhalogin.getText().equals("")) { mensagem = mensagem + "O campo confirmar senha é obrigatório!\n"; retorno = false; } if (retorno == false) { JOptionPane.showMessageDialog(null, mensagem); } return retorno; }
//Criando um metodo vazio
private void validatela(String acao){/*Quando clicar no botao novo, vai ser feito o comando a baixo, estamos fazendo uma função e comparando
o metodo valida tela, depois iremos chamar essa função no actionperformed de cada botao*/if (acao.equals("inicio")){
//BOTÃOES btadicionar.setEnabled(true); btsalvar.setEnabled(false); btsair.setEnabled(true); bteditar.setEnabled(false); btcancelar.setEnabled(false); btexcluir.setEnabled(false); //CAMPOS cnomefuncionario.setEnabled(false); cPermissao.setEnabled(false); clogin.setEnabled(false); csenhalogin.setEnabled(false); cconfirmasenhalogin.setEnabled(false); }if (acao.equals("novo")){ //BOTÃOES btadicionar.setEnabled(false); btsalvar.setEnabled(true); btsair.setEnabled(true); bteditar.setEnabled(true); btcancelar.setEnabled(true); //CAMPOS cnomefuncionario.setEnabled(true); cPermissao.setEnabled(!false); clogin.setEnabled(true); csenhalogin.setEnabled(true); cconfirmasenhalogin.setEnabled(true); } else if (acao.equals("editar")){ //BOTÃOES btadicionar.setEnabled(false); btexcluir.setEnabled(false); bteditar.setEnabled(false); btsalvar.setEnabled(true); btcancelar.setEnabled(true); btsair.setEnabled(true); //CAMPOS cnomefuncionario.setEnabled(!false); clogin.setEnabled(!false); csenhalogin.setEnabled(!false); cconfirmasenhalogin.setEnabled(!false); cPermissao.setEnabled(!false);
}
else if(acao.equals("selecionar")){
btadicionar.setEnabled(false);
btexcluir.setEnabled(true);
bteditar.setEnabled(true);
btsair.setEnabled(true);
btcancelar.setEnabled(false);
btsalvar.setEnabled(false);cnomefuncionario.setEnabled(false); cconfirmasenhalogin.setEnabled(false); cnomefuncionario.setEnabled(false); csenhalogin.setEnabled(false); }
}
public void montatabela(){
listausuario = HibernateUtil.getSession()
.createCriteria(Usuario.class).list();
DefaultTableModel Colunas = new DefaultTableModel();
Colunas.addColumn("Nome do Usuário");
Colunas.addColumn("Usuário");
Colunas.addColumn("Senha");
Colunas.addColumn("Confirmação da Senha");
Colunas.addColumn("Permissão");
for(Usuario u: listausuario ){
Colunas.addRow(new Object[]{u.getNomefuncionario(),u.getLogin(),u.getSenhalogin()
,u.getConfirmasenhalogin(), u.getPermissao()});
}
jTableUsuario.setModel(Colunas);
}@SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); btsair = new javax.swing.JButton(); btcancelar = new javax.swing.JButton(); jLabel13 = new javax.swing.JLabel(); csenhalogin = new javax.swing.JPasswordField(); jLabel16 = new javax.swing.JLabel(); jLabel18 = new javax.swing.JLabel(); cconfirmasenhalogin = new javax.swing.JPasswordField(); jLabel2 = new javax.swing.JLabel(); cPermissao = new javax.swing.JComboBox(); cnomefuncionario = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); clogin = new javax.swing.JTextField(); btadicionar = new javax.swing.JButton(); btexcluir = new javax.swing.JButton(); bteditar = new javax.swing.JButton(); btsalvar = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTableUsuario = new javax.swing.JTable(); cBusca = new javax.swing.JTextField(); btBusca = new javax.swing.JButton(); setClosable(true); setIconifiable(true); setMaximizable(true); setTitle("Cadastro de Usuários"); btsair.setText("Sair"); btsair.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btsairActionPerformed(evt); } }); btcancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/btcancelar.png"))); // NOI18N btcancelar.setText("Cancelar"); btcancelar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btcancelarActionPerformed(evt); } }); jLabel13.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel13.setText("Usuário:"); jLabel16.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel16.setText("Senha:"); jLabel18.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel18.setText("Confirma Senha:"); jLabel2.setText("Permissão:"); cPermissao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Gerencial", "Administrativo" })); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N jLabel1.setText("Nome:"); btadicionar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/btadicionar.png"))); // NOI18N btadicionar.setText("Add"); btadicionar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btadicionarActionPerformed(evt); } }); btexcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/btdelete.png"))); // NOI18N btexcluir.setText("Excluir"); btexcluir.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btexcluirActionPerformed(evt); } }); bteditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/bteditar.png"))); // NOI18N bteditar.setText("Editar"); bteditar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { bteditarActionPerformed(evt); } }); btsalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/btsalvar.png"))); // NOI18N btsalvar.setText("Salvar"); btsalvar.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btsalvarActionPerformed(evt); } }); jTableUsuario.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" } )); jTableUsuario.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { jTableUsuarioMouseClicked(evt); } }); jScrollPane1.setViewportView(jTableUsuario); btBusca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/btpesquisar.png"))); // NOI18N btBusca.setText("Buscar"); btBusca.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btBuscaActionPerformed(evt); } }); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap(29, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(btadicionar, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(btexcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(bteditar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btsalvar) .addGap(9, 9, 9) .addComponent(btcancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(154, 154, 154) .addComponent(btsair, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(58, 58, 58)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(cnomefuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 371, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(cPermissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(cBusca, javax.swing.GroupLayout.PREFERRED_SIZE, 486, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel13) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(clogin, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(jLabel16) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(csenhalogin, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jLabel18))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(cconfirmasenhalogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(btBusca) .addGap(45, 45, 45))))) .addGap(83, 83, 83)))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jScrollPane1) .addContainerGap()) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cconfirmasenhalogin, csenhalogin}); jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btadicionar, bteditar, btsalvar}); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(2, 2, 2) .addComponent(btsair)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btcancelar) .addComponent(btadicionar) .addComponent(btexcluir) .addComponent(bteditar) .addComponent(btsalvar))) .addGap(38, 38, 38) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cPermissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(cnomefuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(28, 28, 28) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel16) .addComponent(jLabel13) .addComponent(clogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel18) .addComponent(cconfirmasenhalogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(csenhalogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(35, 35, 35) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(cBusca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btBusca)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btadicionar, bteditar, btexcluir, btsalvar}); jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btcancelar, btsair}); 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() .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); }// </editor-fold> private void btBuscaActionPerformed(java.awt.event.ActionEvent evt) { } private void btadicionarActionPerformed(java.awt.event.ActionEvent evt) { validatela("novo"); } private void btsalvarActionPerformed(java.awt.event.ActionEvent evt) { if (validaCampos()){ usu.setNomefuncionario(cnomefuncionario.getText()); usu.setLogin(clogin.getText()); usu.setSenhalogin(csenhalogin.getText()); usu.setConfirmasenhalogin(cconfirmasenhalogin.getText()); usu.setPermissao((String)cPermissao.getSelectedItem()); HibernateUtil.beginTransaction(); HibernateUtil.getSession().merge(usu); HibernateUtil.commitTransaction(); HibernateUtil.closeSession(); montatabela(); limpaCampos(); validatela("inicio"); } } private void btcancelarActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: limpaCampos(); validatela("inicio"); } private void btsairActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose(); // TODO add your handling code here:
}private void jTableUsuarioMouseClicked(java.awt.event.MouseEvent evt) { usu = listausuario.get(jTableUsuario.getSelectedRow()); cnomefuncionario.setText(usu.getNomefuncionario()); clogin.setText(usu.getLogin()); csenhalogin.setText(usu.getSenhalogin()); cconfirmasenhalogin.setText(usu.getConfirmasenhalogin()); validatela("selecionar"); } private void bteditarActionPerformed(java.awt.event.ActionEvent evt) { validatela("editar"); } private void btexcluirActionPerformed(java.awt.event.ActionEvent evt) { Object [] opcoes={"Sim","Não"}; int x = JOptionPane.showOptionDialog(null,"Tem certeza que deseja excluir o " + "usuário","Atenção",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,opcoes,opcoes[0]); if(x==JOptionPane.YES_OPTION){ HibernateUtil.beginTransaction(); HibernateUtil.getSession().delete(usu); HibernateUtil.commitTransaction(); HibernateUtil.closeSession(); } montatabela(); limpaCampos(); validatela("inicio"); }
Mensagens: 1
Participantes: 1