Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package sistemaVendas; import java.io.File; import java.io.IOException; import java.util.List; import javax.swing.JDesktopPane; import javax.swing.table.DefaultTableModel; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; /** * * @author BRUNO */ public class AlterarCliente extends javax.swing.JInternalFrame { /** * Creates new form AlterarCliente */ JDesktopPane telas; public AlterarCliente(JDesktopPane tela) { telas = tela; initComponents(); model.setNumRows(0); tblAteracaoClientes.setModel(model); lblSelecionarItemTabela.setVisible(false); tblAteracaoClientes.getColumnModel().getColumn(0).setMaxWidth(35); tblAteracaoClientes.getColumnModel().getColumn(1).setMaxWidth(250); tblAteracaoClientes.getColumnModel().getColumn(1).setMinWidth(250); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { lblProcurarAlteracaoCliente = new javax.swing.JLabel(); lblIdAlteracaoCliente = new javax.swing.JLabel(); lblNomeAlteracaoCliente = new javax.swing.JLabel(); lblCpfAlteracaoCliente = new javax.swing.JLabel(); txtIdAlteracaoCliente = new javax.swing.JTextField(); txtNomeAlteracaoCliente = new javax.swing.JTextField(); txtCpfAlteracaoCliente = new javax.swing.JFormattedTextField(); jScrollPane1 = new javax.swing.JScrollPane(); tblAteracaoClientes = new javax.swing.JTable(); btnCancelarAlteracaoCliente = new javax.swing.JButton(); lblSelecionarItemTabela = new javax.swing.JLabel(); setClosable(true); setResizable(true); setTitle("Alterao de Dados do Cliente"); setPreferredSize(new java.awt.Dimension(700, 470)); lblProcurarAlteracaoCliente.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N lblProcurarAlteracaoCliente.setText("Procurar"); lblIdAlteracaoCliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblIdAlteracaoCliente.setText("ID:"); lblNomeAlteracaoCliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblNomeAlteracaoCliente.setText("Nome:"); lblCpfAlteracaoCliente.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N lblCpfAlteracaoCliente.setText("CPF:"); txtIdAlteracaoCliente.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtIdAlteracaoClienteKeyReleased(evt); } }); txtNomeAlteracaoCliente.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtNomeAlteracaoClienteKeyReleased(evt); } }); try { txtCpfAlteracaoCliente.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory( new javax.swing.text.MaskFormatter("###.###.###-##"))); } catch (java.text.ParseException ex) { ex.printStackTrace(); } txtCpfAlteracaoCliente.addKeyListener(new java.awt.event.KeyAdapter() { public void keyReleased(java.awt.event.KeyEvent evt) { txtCpfAlteracaoClienteKeyReleased(evt); } }); tblAteracaoClientes .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" })); tblAteracaoClientes.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tblAteracaoClientesMouseClicked(evt); } public void mouseEntered(java.awt.event.MouseEvent evt) { tblAteracaoClientesMouseEntered(evt); } public void mouseExited(java.awt.event.MouseEvent evt) { tblAteracaoClientesMouseExited(evt); } }); jScrollPane1.setViewportView(tblAteracaoClientes); btnCancelarAlteracaoCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/cancel.png"))); // NOI18N btnCancelarAlteracaoCliente.setText("Cancelar"); btnCancelarAlteracaoCliente.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnCancelarAlteracaoClienteActionPerformed(evt); } }); lblSelecionarItemTabela.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N lblSelecionarItemTabela.setText("Selecione o cliente na tabela para realizar as alteraes!!!"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblSelecionarItemTabela).addGap(71, 71, 71) .addComponent(btnCancelarAlteracaoCliente).addGap(46, 46, 46)) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addComponent(lblIdAlteracaoCliente) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtIdAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lblNomeAlteracaoCliente) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtNomeAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE) .addComponent(lblCpfAlteracaoCliente) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txtCpfAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(65, 65, 65)) .addGroup(layout.createSequentialGroup().addComponent(jScrollPane1).addContainerGap()))) .addGroup(layout.createSequentialGroup().addGap(300, 300, 300) .addComponent(lblProcurarAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblProcurarAlteracaoCliente) .addGap(32, 32, 32) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblIdAlteracaoCliente).addComponent(lblNomeAlteracaoCliente) .addComponent(lblCpfAlteracaoCliente) .addComponent(txtIdAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtNomeAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(txtCpfAlteracaoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(43, 43, 43) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnCancelarAlteracaoCliente).addContainerGap()) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblSelecionarItemTabela) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnCancelarAlteracaoClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarAlteracaoClienteActionPerformed this.dispose(); }//GEN-LAST:event_btnCancelarAlteracaoClienteActionPerformed private void tblAteracaoClientesMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAteracaoClientesMouseEntered lblSelecionarItemTabela.setVisible(true); }//GEN-LAST:event_tblAteracaoClientesMouseEntered private void tblAteracaoClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAteracaoClientesMouseClicked if (evt.getClickCount() == 2) { int i = tblAteracaoClientes.getSelectedRow(); String id = (String) tblAteracaoClientes.getValueAt(i, 0); String nome = (String) tblAteracaoClientes.getValueAt(i, 1); String sexo = (String) tblAteracaoClientes.getValueAt(i, 2); String idade = (String) tblAteracaoClientes.getValueAt(i, 3); String telefone = (String) tblAteracaoClientes.getValueAt(i, 4); String cpf = (String) tblAteracaoClientes.getValueAt(i, 5); AlterarClienteProcurado clienteSolicitado = new AlterarClienteProcurado(telas); clienteSolicitado.atualizar(id, nome, sexo, idade, telefone, cpf); telas.add(clienteSolicitado); clienteSolicitado.setVisible(true); } }//GEN-LAST:event_tblAteracaoClientesMouseClicked private void tblAteracaoClientesMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAteracaoClientesMouseExited lblSelecionarItemTabela.setVisible(false); }//GEN-LAST:event_tblAteracaoClientesMouseExited private void txtIdAlteracaoClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdAlteracaoClienteKeyReleased procurar(txtIdAlteracaoCliente.getText(), "", ""); }//GEN-LAST:event_txtIdAlteracaoClienteKeyReleased private void txtNomeAlteracaoClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNomeAlteracaoClienteKeyReleased procurar("", txtNomeAlteracaoCliente.getText().toUpperCase(), ""); }//GEN-LAST:event_txtNomeAlteracaoClienteKeyReleased private void txtCpfAlteracaoClienteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCpfAlteracaoClienteKeyReleased procurar("", "", txtCpfAlteracaoCliente.getText()); }//GEN-LAST:event_txtCpfAlteracaoClienteKeyReleased File diretorio = new File("Bancos de Dados"); File arquivo = new File(diretorio, "BancoCliente.xml"); Document doc; Element root; public void procurar(String id, String nome, String cpf) { model.setNumRows(0); try { SAXBuilder builder = new SAXBuilder(); doc = builder.build(arquivo); root = (Element) doc.getRootElement(); List<Element> clientes = root.getChildren(); String dados[] = new String[6]; for (int i = 0; i < clientes.size(); i++) { Element cliente = clientes.get(i); dados[0] = cliente.getAttributeValue("id"); dados[1] = cliente.getChildText("nome").toUpperCase(); dados[2] = cliente.getChildText("sexo"); dados[3] = cliente.getChildText("idade"); dados[4] = cliente.getChildText("telefone"); dados[5] = cliente.getChildText("cpf"); if (id.equals("") && nome.equals("") && cpf.equals("") || //Mostar todos se forem nulls !id.equals("") && !nome.equals("") && !cpf.equals("") && dados[0].equals(id) && dados[1].contains(nome) && dados[5].contains(cpf) || //Mostrar pelos 3 campos juntos !id.equals("") && !nome.equals("") && dados[0].equals(id) && dados[1].contains(nome) || //Mostrar pelo id e nome !nome.equals("") && !cpf.equals("") && dados[1].contains(nome) && dados[5].contains(cpf) || //Mostrar pelo nome e cpf !id.equals("") && dados[0].equals(id) || //Mostrar pelo id !nome.equals("") && dados[1].contains(nome) || //Mostrar pelo nome !cpf.equals("") && dados[5].contains(cpf)) { //Mostrar pelo cpf model.addRow(new Object[] { dados[0], dados[1], dados[2], dados[3], dados[4], dados[5] }); } } } catch (JDOMException | IOException e) { } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnCancelarAlteracaoCliente; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblCpfAlteracaoCliente; private javax.swing.JLabel lblIdAlteracaoCliente; private javax.swing.JLabel lblNomeAlteracaoCliente; private javax.swing.JLabel lblProcurarAlteracaoCliente; private javax.swing.JLabel lblSelecionarItemTabela; private javax.swing.JTable tblAteracaoClientes; private javax.swing.JFormattedTextField txtCpfAlteracaoCliente; private javax.swing.JTextField txtIdAlteracaoCliente; private javax.swing.JTextField txtNomeAlteracaoCliente; // End of variables declaration//GEN-END:variables private DefaultTableModel model = new DefaultTableModel(new Object[][] {}, new String[] { "ID", "Nome", "Sexo", "Idade", "Telefone", "CPF" }) { public boolean isCellEditable(int row, int col) { return false; } }; }