Prezado Carlos,
Até que não seria lógica não. O problema mesmo é: como fazer o campo referente a entrada de dados referente ao cpf, fazer a chamada da rotina de validação. Essa rotina eu já peguei.
Veja abaixo o meu código em que eu faço a alteração de dados. Aqui eu chamo o registro para consultar e ao se apresentar na tela, eu posso fazer alterações em qualquer campo. como eu não tenho o cpf cadastrado, o campo vai estar em branco. Aí, eu digito o cpf e quero que ele seja válido. enquanto o número digitado não for válido, eu quero que o cursor se posicione no inicio deste campo para que eu corrija o número digitado. Ok?
Veja o meu código: Como eu nunca fiz isso e quero aprender, gostaria de sua ajuda alterando o meu próprio código para que sirva de modelo e que eu possa aprender através dele.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Alterar extends JFrame {
JButton botao, botao2;
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField t1, t2, t3, t4, t5, t6, t7, t8;
String result;
int num;
public Alterar(){
super ("Alteração de Registros");
Container tela = getContentPane();
tela.setLayout(null);
l1 = new JLabel("Código"); t1 = new JTextField(5);
l2 = new JLabel("Nome"); t2 = new JTextField(50);
l3 = new JLabel("Idade"); t3 = new JTextField(2);
l4 = new JLabel("CPF"); t4 = new JTextField(14);
l5 = new JLabel("RG"); t5 = new JTextField(12);
l6 = new JLabel("Data Nascimento"); t6 = new JTextField(10);
l7 = new JLabel("Salario"); t7 = new JTextField(15);
l8 = new JLabel("Deficiente"); t8 = new JTextField(5);
l1.setBounds(20, 30, 80, 20); t1.setBounds(100, 30,150, 20);
l2.setBounds(20, 70, 80, 20); t2.setBounds(100, 70,300, 20);
l3.setBounds(20,110, 80, 20); t3.setBounds(100,110, 30, 20);
l4.setBounds(195,110,40, 20); t4.setBounds(240,110,160, 20);
l5.setBounds(20,150, 80, 20); t5.setBounds(100,150, 80, 20);
l6.setBounds(195,150,90, 20); t6.setBounds(280,150,120, 20);
l7.setBounds(20,190, 80, 20); t7.setBounds(100,190, 70, 20);
l8.setBounds(20,230, 80, 20); t8.setBounds(100,230, 70, 20);
botao = new JButton("Consultar Registro"); botao.setBounds(20, 270, 150, 20);
botao2 = new JButton("Alterar Registro"); botao2.setBounds(250, 270, 150, 20);
tela.add(l1); tela.add(t1);
tela.add(l2); tela.add(t2);
tela.add(l3); tela.add(t3);
tela.add(l4); tela.add(t4);
tela.add(l5); tela.add(t5);
tela.add(l6); tela.add(t6);
tela.add(l7); tela.add(t7);
tela.add(l8); tela.add(t8);
tela.add(botao); tela.add(botao2);
botao.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
//String url = "jdbc:odbc:Agenda";
//String usuario = "";
//String senha = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//Connection con;
Connection con =
DriverManager.getConnection("jdbc:odbc:Agenda", "","");
//con = DriverManager.getConnection(url,usuario,senha);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM tabela1 WHERE Codigo ="+ t1.getText());
rs.next();
t2.setText(rs.getString("Nome"));
t3.setText(rs.getString("Idade"));
t4.setText(rs.getString("Cpf"));
t5.setText(rs.getString("strg"));
t6.setText(rs.getString("Dn"));
///**************************************************************
///**************************************************************
/// strrg.addActionListener(new java.awt.event.ActionListener() {
/// public void actionPerformed(java.awt.event.ActionEvent evt) {
/// strgActionPerformed(evt);
/// }
/// });
///**************************************************************
///************************************************************** t6.setText(rs.getString("Dn"));
t7.setText(rs.getString("Sal"));
num=Integer.parseInt(rs.getString("Def"));
if (num == 1)
result = "Sim";
{
if (num == 0)
result = "Não";
}
t8.setText(result);
st.close();
}
catch(Exception event ){
JOptionPane.showMessageDialog(null, "Conexão não estabelecida ou \nPessoa não encontrada \n Verifique se voce digitou o código para consulta", "Mensgaem do programa ", JOptionPane.ERROR_MESSAGE);
}
}});
/// private void strgActionPerformed (java.awt.event.ActionEvent evt) {
/// // aqui vc chama a rotina de validação
/// ValidaCpf cpf = new ValidaCpf();
/// validado=cpf.validacpf(strcpf.getText()); //validacpf
/// if(validado){
/// //faz alguma coisa
/// } else msgErroCpf();
/// }
botao2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
if (t8.getText().equals("sim")) //|| (((t6.getText()).equals("S")))
result = "Sim";
//t8.Text("Sim");
if (t8.getText().equals("S")) //|| (((t6.getText()).equals("S")))
result = "Sim";
// t8.Text("Sim");
if (t8.getText().equals("n")) //|| (((t6.getText()).equals("n")))
result = "Não";
// t8.Text("Não");
if (t8.getText().equals("N")) //|| (((t6.getText()).equals("N")))
result = "Não";
//t8.Text("Não");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection("jdbc:odbc:Agenda", "","");
Statement st = con.createStatement();
//st.executeUpdate("UPDATE.Tabela1 SET Nome ='"+t2.getText()+"', Idade = "+t3.getText()+",Dn=#"+t4.getText()+"#,Sal="+t5.getText()+",Def="+ result+" WHERE Codigo = "+t1.getText());
st.executeUpdate("UPDATE.Tabela1 SET Nome ='"+t2.getText()+"', Idade = "+t3.getText()+", Cpf = "+t4.getText()+", strg = "+t5.getText()+", Dn=#"+t6.getText()+"#,Sal="+t7.getText()+" WHERE Codigo = "+t1.getText());
JOptionPane.showMessageDialog(null,"Registro Alterado com sucesso!...", "Mensagem do Programa", JOptionPane.INFORMATION_MESSAGE);
t1.requestFocus();
st.close();
con.close();
}
catch(Exception event){
JOptionPane.showMessageDialog(null, "Conexão não estabelecida", "Mensagem do Programa", JOptionPane.ERROR_MESSAGE);
}
}});
setSize(450,350);
setVisible(true);
setLocationRelativeTo(null);
}
public static void main (String args[]){
Alterar app = new Alterar();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Atenciosamente,
Edson