Bom tenho que fazer um formulário que envia dados para o servidor via sockets,
o meu problema encontra-se no cliente(onde está o Formulario) queria definir para que os campos
telefone/CPF/CEP só possa ser digitado numeros e se puder com limitação de caracteres, estou usando
o JTextField(se tiver outro meio de criar caixa alternativo ao JTextField, gostaria de saber).
Ah estou usando Eclipse!!
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Client extends JFrame{
private JLabel lbnome, lbcpf, lbdat, lbNac, lbEc, lbEsc, lbSexo, lbPais, lbEstado, lbCidade, lbBairro,
lbEnd, lbCep, lbEmail, lbTel, lbTitulo;
private JTextField nome, cpf, dia, ano, nac, pais, estado, cidade, bairro, end, cep, email, tel, ddd;
private JTextArea displayArea;
private JButton bt1;
private JButton bt2;
private JComboBox datNas, ec, esc, sexo, tipoTel;
private String strMeses[] = { "","Jan","Fev","Mar",
"Abr","Mai","Jun","Jul","Ago","Set","Out","Nov","Dez" };
private String strEc[] = { "","Solteiro","Casado","Divorciado","Viúvo",};
private String strEsc[] = { "","Ensino Fundamental (1º Grau)","Ensino Médio (2º Grau)",
"Ensino Médio Profissionalizante (2º Tecnico)","Superior","Pós-Graduação","Mestrado","Doutorado"};
private String strSexo[] = { "","Masculino","Feminino"};
private String strTel[] = { "","Residencial","Comercial","Celular"};
private DatagramPacket sendPacket, receivePacket;
private DatagramSocket socket;
private int num_perguntas=0;
protected Socket s;
public Client()
{
super ("Cliente");
setLayout( null );
Container container = getContentPane();
lbTitulo = new JLabel("CADASTRO DE DADOS DO CLIENTE ");
add(lbTitulo);
lbTitulo.setBounds(5, 5, 500, 20);
lbTitulo = new JLabel("STATUS DE ENVIO ");
add(lbTitulo);
lbTitulo.setBounds(10, 530, 500, 20);
//Nome//
lbnome = new JLabel("Nome Completo: ");
add(lbnome);
lbnome.setBounds(10, 40, 100, 20);
nome = new JTextField("");
nome.setBounds(110, 40, 250, 20);
add(nome);
//CPF//
lbcpf = new JLabel("CPF: ");
add(lbcpf);
lbcpf.setBounds(78, 70, 100, 20); // col, lin, larg, alt
cpf = new JTextField("");
cpf.setBounds(110, 70, 100, 20);
add(cpf);
//DataNascimento//
lbdat = new JLabel("Data de Nasc.: ");
add(lbdat);
lbdat.setBounds(21, 100, 100, 20);
dia = new JTextField("");
dia.setBounds(110, 100, 25, 20);
add(dia);
datNas = new JComboBox(strMeses);
datNas.setBounds(140, 100, 50, 20);
add( datNas );
ano = new JTextField("");
ano.setBounds(195, 100, 35, 20);
add(ano);
//Nacionalidade//
lbNac = new JLabel("Nacionalidade: ");
add(lbNac);
lbNac.setBounds(20, 130, 100, 20); // col, lin, larg, alt
nac = new JTextField("");
nac.setBounds(110, 130, 100, 20);
add(nac);
//EstadoCivil//
lbEc = new JLabel("Estado Civil: ");
add(lbEc);
lbEc.setBounds(34, 160, 100, 20); // col, lin, larg, alt
ec = new JComboBox(strEc);
ec.setBounds(110, 160, 100, 20);
add( ec );
//Escolaridade//
lbEsc = new JLabel("Escolaridade: ");
add(lbEsc);
lbEsc.setBounds(25, 190, 100, 20); // col, lin, larg, alt
esc = new JComboBox(strEsc);
esc.setBounds(110, 190, 250, 20);
add( esc );
//Sexo//
lbSexo = new JLabel("Sexo: ");
add(lbSexo);
lbSexo.setBounds(70, 220, 100, 20); // col, lin, larg, alt
sexo = new JComboBox(strSexo);
sexo.setBounds(110, 220, 100, 20);
add( sexo );
//País//
lbPais = new JLabel("País: ");
add(lbPais);
lbPais.setBounds(74, 250, 100, 20); // col, lin, larg, alt
pais = new JTextField("");
pais.setBounds(110, 250, 100, 20);
add(pais);
//Estado//
lbEstado = new JLabel("Estado: ");
add(lbEstado);
lbEstado.setBounds(60, 280, 100, 20); // col, lin, larg, alt
estado = new JTextField("");
estado.setBounds(110, 280, 150, 20);
add(estado);
//Cidade//
lbCidade = new JLabel("Cidade: ");
add(lbCidade);
lbCidade.setBounds(60, 310, 100, 20); // col, lin, larg, alt
cidade = new JTextField("");
cidade.setBounds(110, 310, 150, 20);
add(cidade);
//Bairro//
lbBairro = new JLabel("Bairro: ");
add(lbBairro);
lbBairro.setBounds(65, 340, 100, 20); // col, lin, larg, alt
bairro = new JTextField("");
bairro.setBounds(110, 340, 150, 20);
add(bairro);
//Endereço//
lbEnd = new JLabel("Endereço: ");
add(lbEnd);
lbEnd.setBounds(46, 370, 100, 20); // col, lin, larg, alt
end = new JTextField("");
end.setBounds(110, 370, 250, 20);
add(end);
//Cep//
lbCep = new JLabel("CEP: ");
add(lbCep);
lbCep.setBounds(77, 400, 100, 20); // col, lin, larg, alt
cep = new JTextField("");
cep.setBounds(110, 400, 100, 20);
add(cep);
//Email//
lbEmail = new JLabel("Email: ");
add(lbEmail);
lbEmail.setBounds(68, 430, 100, 20); // col, lin, larg, alt
email = new JTextField("");
email.setBounds(110, 430, 250, 20);
add(email);
//Telefone//
lbTel = new JLabel("Telefone: ");
add(lbTel);
lbTel.setBounds(50, 460, 100, 20);
ddd = new JTextField("");
ddd.setBounds(110, 460, 30, 20);
add(ddd);
tel = new JTextField("");
tel.setBounds(145, 460, 80, 20);
add(tel);
tipoTel = new JComboBox(strTel);
tipoTel.setBounds(250, 460, 100, 20);
add( tipoTel );
// criar os demais componentes
bt1 = new JButton("Enviar");
bt1.setBounds(10, 610, 100, 20);
add(bt1);
bt1.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent event) {
try{
displayArea.append(
"\n Enviando comando para o servidor. " + "\n");
String message = "\n"+
"Nome: "+nome.getText()+"\n"+
"CPF: "+cpf.getText()+"\n"+
"Data de Nascimento: "+dia.getText()+" / "+datNas.getSelectedItem()+" / "+ano.getText()+"\n"+
"Nacionalidade: "+nac.getText()+"\n"+
"Estado Civil: "+ec.getSelectedItem()+"\n"+
"Escolaridade: "+esc.getSelectedItem()+"\n"+
"Sexo: "+sexo.getSelectedItem()+"\n"+
"País: "+pais.getText()+"\n"+
"Estado: "+estado.getText()+"\n"+
"Cidade: "+cidade.getText()+"\n"+
"Bairro: "+bairro.getText()+"\n"+
"Endereço: "+end.getText()+"\n"+
"CEP: "+cep.getText()+"\n"+
"Email: "+email.getText()+"\n"+
"Telefone: "+ddd.getText()+" - "+tel.getText()+" ("+tipoTel.getSelectedItem()+") ";
;
byte data[] = message.getBytes();
sendPacket = new DatagramPacket(
data, data.length,
InetAddress.getLocalHost(), 5000 );
socket.send(sendPacket);
displayArea.append("Pacote enviado. \n");
displayArea.setCaretPosition(
displayArea.getText().length());
}
catch (IOException ioException){
displayArea.append(
ioException.toString() + "\n" );
ioException.printStackTrace();
}
}
}
);
bt2 = new JButton("Fim");
bt2.setBounds(250, 610, 100, 20);
add(bt2);
bt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
// fim chamada para addActionLintener
displayArea = new JTextArea();
JScrollPane s = new JScrollPane(displayArea);
s.setBounds(10, 550, 350, 50);
container.add(s);
setSize (400, 700);
setVisible(true);
try{
socket = new DatagramSocket();
}
catch (SocketException socketException){
socketException.printStackTrace();
System.exit(1);
}
} // fim do construtor do cliente
public void waitForPackets(){
while (true){
try{
byte data[] = new byte [1000];
receivePacket = new DatagramPacket(data, data.length);
socket.receive(receivePacket);
num_perguntas++;
displayPacket();
}
catch (IOException ioException){
displayArea.append (
ioException.toString() + "\n");
ioException.printStackTrace();
}
}
}
private void displayPacket(){
displayArea.append ("\n" + new String (receivePacket.getData(), 0,
receivePacket.getLength() ));
displayArea.setCaretPosition(
displayArea.getText().length() );
}
public static void main ( String args[]){
Client application = new Client();
application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
application.waitForPackets();
}
}