Bom dia Pessoal
Ao preencher os JTextField com máscara, com o método .setText();
campos com máscara o windos apita (“pim”) como se estivesse terminado de preencher a mão
agora, quando executo o evento abaixo, mudando de linha da JTable, com todos os JTextField vazios, o windows sinaliza do mesmo jeito “pim”
me ajudem por favor, não sei como resolver isso, é invisível, o eclipse não aponta o erro
// esse código é o evento da JTable quando mudo de linha e pego as linhas e adiciono nos JTEXTFIELD por meio do .setText();
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if(evt.getValueIsAdjusting())
return;
int selected = table.getSelectedRow();
if (table.isRowSelected(selected)) {
clearFields();
String [] objLinha1 = model.getLinha(selected);
preencheCamposTexto(objLinha1);
}
}
});
//código completo da minha janela JFRAME
package visualizacao;
import java.awt.EventQueue;
public class PessoaView extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
private PessoaTableModel model;
private JTextField txtNome;
private JTextField txtLogradouro;
private JTextField txtComplemento;
private JTextField txtBairro;
private JTextField txtCidade;
private JTextField txtUf;
private JTextField txtContato;
private JTextField txtCelular2;
private JTextField txtFax;
private JTextField txtFoneResidencial;
private JTextField txtFoneComercial;
private JTextField txtCelular1;
private JTextField txtId;
private JTextField txtEmail;
private JTextField txtSite;
private JTextField txtEmpresa;
private JTextField txtCaixaPostal;
private JTextField txtRg;
private JTextField txtIe;
private boolean modoAlteracao;
private JFormattedTextField txtCEP;
private JFormattedTextField txtCpf;
private JFormattedTextField txtCnpj;
private JFormattedTextField txtDataNascimento;
private PessoaController objController;
private JTextArea txtrObservacao;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PessoaView frame = new PessoaView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws ParseException
*/
public PessoaView() throws ParseException {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(PessoaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(PessoaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(PessoaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(PessoaView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 964, 592);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[681px,grow]", "[525.00px]"));
final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
contentPane.add(tabbedPane, "cell 0 0,grow");
JPanel panel = new JPanel();
tabbedPane.addTab("Consulta", null, panel, null);
panel.setLayout(new MigLayout("", "[25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px]", "[25px][25px][grow]"));
JButton btnIncluir = new JButton("Incluir");
btnIncluir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
enableFields();
clearFields();
tabbedPane.setSelectedIndex(1);
txtNome.requestFocus();
modoAlteracao = false;
}
});
panel.add(btnIncluir, "cell 0 0");
JButton btnAlterar = new JButton("Alterar");
btnAlterar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selecionado = table.getSelectedRow();
if(table.isRowSelected(selecionado)) {
tabbedPane.setSelectedIndex(1);
enableFields();
txtNome.requestFocus();
modoAlteracao = true;
} else {
JOptionPane.showMessageDialog(null, "Selecione uma Pessoa da tabela!");
}
}
});
panel.add(btnAlterar, "cell 1 0");
JButton btnExcluir = new JButton("Excluir");
btnExcluir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int selecionado = table.getSelectedRow();
String [] objLinha = new String[23];
objLinha = model.getLinha(selecionado);
int escolha = JOptionPane.showConfirmDialog(null, "Confirma a exclusão de: " + objLinha[1], "Confirmação de Exclusão", JOptionPane.YES_NO_OPTION);
if (escolha == JOptionPane.YES_OPTION) {
objController.excluir(Integer.valueOf(objLinha[0]));//informa o controlador o ID do objeto. Falta ajustar com retorno boolean
model.removeLinha(selecionado);
clearFields();
disableFields();
}
}
});
panel.add(btnExcluir, "cell 2 0");
JButton btnSair = new JButton("Sair");
btnSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
panel.add(btnSair, "cell 0 1,growx");
JScrollPane scrollPane = new JScrollPane();
panel.add(scrollPane, "cell 0 2 27 1,grow");
objController = new PessoaController();
model = new PessoaTableModel(objController.recuperaObjetos());
table = new JTable();
scrollPane.setColumnHeaderView(table);
scrollPane.setViewportView(table);
table.setModel(model);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if(evt.getValueIsAdjusting())
return;
int selected = table.getSelectedRow();
if (table.isRowSelected(selected)) {
//clearFields();
String [] objLinha1 = model.getLinha(selected);
preencheCamposTexto(objLinha1);
}
}
});
JPanel panel_1 = new JPanel();
tabbedPane.addTab("Atualização", null, panel_1, null);
panel_1.setLayout(new MigLayout("", "[25px][25px][25px][25px][][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px]", "[25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px][25px]"));
JLabel lblNome = new JLabel("ID");
panel_1.add(lblNome, "cell 0 0,alignx trailing");
txtId = new JTextField();
panel_1.add(txtId, "cell 1 0 5 1,alignx left");
txtId.setColumns(10);
txtId.setEnabled(false);
JLabel lblNome_1 = new JLabel("Nome");
panel_1.add(lblNome_1, "cell 0 1,alignx trailing");
txtNome = new JTextField();
panel_1.add(txtNome, "cell 1 1 10 1,growx");
txtNome.setColumns(10);
txtNome.setDocument(new FixedLengthDocument(50));
try {
txtDataNascimento = new JFormattedTextField(new MaskFormatter("##/##/####"));
panel_1.add(txtDataNascimento, "cell 13 1 3 1,growx");
} catch (Exception e) {
}
JLabel lblDataNasc = new JLabel("Data Nasc.");
panel_1.add(lblDataNasc, "cell 11 1 2 1,alignx trailing");
JLabel lblLogradouro = new JLabel("Logradouro");
panel_1.add(lblLogradouro, "cell 0 2,alignx trailing");
txtLogradouro = new JTextField();
panel_1.add(txtLogradouro, "cell 1 2 7 1,growx");
txtLogradouro.setColumns(10);
txtLogradouro.setDocument(new FixedLengthDocument(40));
JLabel lblComplemento = new JLabel("Complemento");
panel_1.add(lblComplemento, "cell 0 3,alignx trailing");
txtComplemento = new JTextField();
panel_1.add(txtComplemento, "cell 1 3 7 1,growx");
txtComplemento.setColumns(10);
txtComplemento.setDocument(new FixedLengthDocument(20));// limita o número de caracteres inseridos
JLabel lblCaixaPostal = new JLabel("Caixa Postal");
panel_1.add(lblCaixaPostal, "cell 8 3 3 1,alignx right");
txtCaixaPostal = new JTextField();
panel_1.add(txtCaixaPostal, "cell 11 3 4 1,growx");
txtCaixaPostal.setColumns(10);
txtCaixaPostal.setDocument(new FixedLengthDocument(5));
JLabel lblBairro = new JLabel("Bairro");
panel_1.add(lblBairro, "cell 0 4,alignx trailing");
txtBairro = new JTextField();
panel_1.add(txtBairro, "cell 1 4 7 1,growx");
txtBairro.setColumns(10);
txtBairro.setDocument(new FixedLengthDocument(20));
JLabel lblCep = new JLabel("CEP");
panel_1.add(lblCep, "cell 8 4,alignx trailing");
try {
MaskFormatter cep = new MaskFormatter("#####-###");
txtCEP = new JFormattedTextField(cep);
panel_1.add(txtCEP, "cell 9 4 4 1,growx");
} catch (Exception e) {
}
JLabel lblCidade = new JLabel("Cidade");
panel_1.add(lblCidade, "cell 0 5,alignx trailing");
txtCidade = new JTextField();
panel_1.add(txtCidade, "cell 1 5 7 1,growx");
txtCidade.setColumns(10);
txtCidade.setDocument(new FixedLengthDocument(35));
JLabel lblUf = new JLabel("UF");
panel_1.add(lblUf, "cell 8 5,alignx trailing");
txtUf = new JTextField();
panel_1.add(txtUf, "cell 9 5,growx");
txtUf.setColumns(10);
txtUf.setDocument(new FixedLengthDocument(2));
JLabel lblContato = new JLabel("Contato");
panel_1.add(lblContato, "cell 0 6,alignx trailing");
txtContato = new JTextField();
panel_1.add(txtContato, "cell 1 6 9 1,growx");
txtContato.setColumns(10);
txtContato.setDocument(new FixedLengthDocument(40));
JLabel lblEmpresa = new JLabel("Empresa");
panel_1.add(lblEmpresa, "cell 0 7,alignx right");
txtEmpresa = new JTextField();
panel_1.add(txtEmpresa, "cell 1 7 9 1,growx");
txtEmpresa.setColumns(10);
txtEmpresa.setDocument(new FixedLengthDocument(50));
try {
//MaskFormatter cpf= new MaskFormatter("###.###.###-##");
txtCpf = new JFormattedTextField(new MaskFormatter("###.###.###-##"));
//txtCpf.setDocument(new FixedLengthDocument(14));//como esta linha o windows não apita
panel_1.add(txtCpf, "cell 11 7 5 1,growx");
} catch (Exception e) {
}
JLabel lblCpf = new JLabel("CPF");
panel_1.add(lblCpf, "cell 10 7,alignx trailing");
JLabel lblFone1 = new JLabel("Fone Residencial");
panel_1.add(lblFone1, "cell 0 8,alignx trailing");
txtFoneResidencial = new JTextField();
panel_1.add(txtFoneResidencial, "cell 1 8 6 1,growx");
txtFoneResidencial.setColumns(10);
txtFoneResidencial.setDocument(new FixedLengthDocument(30));
JLabel lblRg = new JLabel("RG");
panel_1.add(lblRg, "cell 10 8,alignx trailing");
txtRg = new JTextField();
panel_1.add(txtRg, "cell 11 8 5 1,growx");
txtRg.setColumns(10);
txtRg.setDocument(new FixedLengthDocument(10));
JLabel lblCelular = new JLabel("Fone Comercial");
panel_1.add(lblCelular, "cell 0 9,alignx trailing");
txtFoneComercial = new JTextField();
panel_1.add(txtFoneComercial, "cell 1 9 6 1,growx");
txtFoneComercial.setColumns(10);
txtFoneComercial.setDocument(new FixedLengthDocument(30));
try {
txtCnpj = new JFormattedTextField(new MaskFormatter("##.###.###/####-##"));
//txtCnpj = new JFormattedTextField();
//txtCnpj.setDocument(new FixedLengthDocument(18));//com essa linha o windows não apita
panel_1.add(txtCnpj, "cell 11 9 5 1,growx");
} catch (Exception e ) {
}
JLabel lblCnpj = new JLabel("CNPJ");
panel_1.add(lblCnpj, "cell 10 9,alignx trailing");
JLabel lblCelular1 = new JLabel("Celular1");
panel_1.add(lblCelular1, "cell 0 10,alignx trailing");
txtCelular1 = new JTextField();
panel_1.add(txtCelular1, "cell 1 10 6 1,growx");
txtCelular1.setColumns(10);
txtCelular1.setDocument(new FixedLengthDocument(30));
JLabel lblIe = new JLabel("IE");
panel_1.add(lblIe, "cell 10 10,alignx trailing");
txtIe = new JTextField();
panel_1.add(txtIe, "cell 11 10 5 1,growx");
txtIe.setColumns(10);
txtIe.setDocument(new FixedLengthDocument(15));
JLabel lblCelular2 = new JLabel("Celular2");
panel_1.add(lblCelular2, "cell 0 11,alignx trailing");
txtCelular2 = new JTextField();
panel_1.add(txtCelular2, "cell 1 11 6 1,growx");
txtCelular2.setColumns(10);
txtCelular2.setDocument(new FixedLengthDocument(30));
JLabel lblEmail = new JLabel("Email");
panel_1.add(lblEmail, "cell 7 11,alignx trailing");
txtEmail = new JTextField();
panel_1.add(txtEmail, "cell 8 11 8 1,growx");
txtEmail.setColumns(10);
txtEmail.setDocument(new FixedLengthDocument(200));
JLabel lblFax = new JLabel("Fax");
panel_1.add(lblFax, "cell 0 12,alignx trailing");
txtFax = new JTextField();
panel_1.add(txtFax, "cell 1 12 6 1,growx");
txtFax.setColumns(10);
txtFax.setDocument(new FixedLengthDocument(30));
JLabel lblSite = new JLabel("Site");
panel_1.add(lblSite, "cell 7 12,alignx trailing");
txtSite = new JTextField();
panel_1.add(txtSite, "cell 8 12 8 1,growx");
txtSite.setColumns(10);
txtSite.setDocument(new FixedLengthDocument(200));
JLabel lblObservaes = new JLabel("Observa\u00E7\u00F5es");
panel_1.add(lblObservaes, "cell 0 13,alignx right");
JScrollPane scrollPane_1 = new JScrollPane();
panel_1.add(scrollPane_1, "cell 1 13 15 4,grow");
txtrObservacao = new JTextArea();
scrollPane_1.setViewportView(txtrObservacao);
txtrObservacao.setDocument(new FixedLengthDocument(500));
JButton btnGravar = new JButton("Gravar");
btnGravar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String [] objLinha1 = getTodosCamposTexto();
String campoID = objLinha1[0];
int selecionado = table.getSelectedRow();
objLinha1 = objController.salvarERetornar(objLinha1);
if(!objLinha1[0].trim().equals(campoID)) {
model.addLinha(objLinha1);
} else {
model.setLinha(objLinha1, selecionado);
}
disableFields();
tabbedPane.setSelectedIndex(0);
}
});
panel_1.add(btnGravar, "cell 1 17 3 1,growx");
JButton btnNewButton = new JButton("Cancelar");
panel_1.add(btnNewButton, "cell 5 17 3 1,alignx right");
disableFields();
}
public void preencheCamposTexto(String [] objLinha) {
txtId.setText(objLinha[0]);
txtNome.setText(objLinha[1]);
txtDataNascimento.setText(objLinha[2]);
txtLogradouro.setText(objLinha[3]);
txtComplemento.setText(objLinha[4]);
txtBairro.setText(objLinha[5]);
txtCidade.setText(objLinha[6]);
txtCaixaPostal.setText(objLinha[7]);
txtContato.setText(objLinha[8]);
txtEmpresa.setText(objLinha[9]);
txtEmail.setText(objLinha[10]);
txtSite.setText(objLinha[11]);
txtUf.setText(objLinha[12]);
txtCEP.setText(objLinha[13]); // este campo quando habilitado o windows apita
txtFoneResidencial.setText(objLinha[14]);
txtFoneComercial.setText(objLinha[15]);
txtCelular1.setText(objLinha[16]);
txtCelular2.setText(objLinha[17]);
txtFax.setText(objLinha[18]);
txtCnpj.setText(objLinha[19]); //esta linha quando habilitada o windows apita
txtCpf.setText(objLinha[20]); //esta linha quando habilitada o windows apita
txtIe.setText(objLinha[21]);
txtRg.setText(objLinha[22]);
txtrObservacao.setText(objLinha[23]);
}
public String [] getTodosCamposTexto() {
String [] objLinha = new String[24];
if(txtId.getText() != "") {
objLinha[0] = txtId.getText();
}
objLinha[1] = txtNome.getText();
objLinha[2] = txtDataNascimento.getText();
objLinha[3] = txtLogradouro.getText();
objLinha[4] = txtComplemento.getText();
objLinha[5] = txtBairro.getText();
objLinha[6] = txtCidade.getText();
objLinha[7] = txtCaixaPostal.getText();
objLinha[8] = txtContato.getText();
objLinha[9] = txtEmpresa.getText();
objLinha[10] = txtEmail.getText();
objLinha[11] = txtSite.getText();
objLinha[12] = txtUf.getText();
objLinha[13] = txtCEP.getText();
objLinha[14] = txtFoneResidencial.getText();
objLinha[15] = txtFoneComercial.getText();
objLinha[16] = txtCelular1.getText();
objLinha[17] = txtCelular2.getText();
objLinha[18] = txtFax.getText();
objLinha[19] = txtCpf.getText();
objLinha[20] = txtCnpj.getText();
objLinha[21] = txtRg.getText();
objLinha[22] = txtIe.getText();
objLinha[23] = txtrObservacao.getText();
return objLinha;
/*
pessoa.setNome(txtNome.getText());
pessoa.setDataNascimento(txtDataNascimento.getText());
pessoa.setLogradouro(txtLogradouro.getText());
pessoa.setComplemento(txtComplemento.getText());
pessoa.setBairro(txtBairro.getText());
pessoa.setCidade(txtCidade.getText());
pessoa.setCaixaPostal(txtCaixaPostal.getText());
pessoa.setContato(txtContato.getText());
pessoa.setEmpresa(txtEmpresa.getText());
pessoa.setEmail(txtEmail.getText());
pessoa.setSite(txtSite.getText());
pessoa.setUf(txtUf.getText());
pessoa.setCep(txtCEP.getText());
pessoa.setFoneResidencial(txtFoneResidencial.getText());
pessoa.setFoneComercial(txtFoneComercial.getText());
pessoa.setCelular1(txtCelular1.getText());
pessoa.setCelular2(txtCelular2.getText());
pessoa.setFax(txtFax.getText());
pessoa.setNumeroCpf(txtCpf.getText());
pessoa.setNumeroCnpj(txtCnpj.getText());
pessoa.setNumeroRg(txtRg.getText());
pessoa.setNumeroIe(txtIe.getText());
*/
}
public void enableFields() {
txtNome.setEnabled(true);
txtDataNascimento.setEnabled(true);
txtLogradouro.setEnabled(true);
txtComplemento.setEnabled(true);
txtBairro.setEnabled(true);
txtCidade.setEnabled(true);
txtCaixaPostal.setEnabled(true);
txtContato.setEnabled(true);
txtEmpresa.setEnabled(true);
txtEmail.setEnabled(true);
txtSite.setEnabled(true);
txtUf.setEnabled(true);
txtCEP.setEnabled(true);
txtFoneResidencial.setEnabled(true);
txtFoneComercial.setEnabled(true);
txtCelular1.setEnabled(true);
txtCelular2.setEnabled(true);
txtFax.setEnabled(true);
txtCnpj.setEnabled(true);
txtCpf.setEnabled(true);
txtRg.setEnabled(true);
txtIe.setEnabled(true);
txtrObservacao.setEnabled(true);
}
public void disableFields() {
txtNome.setEnabled(false);
txtDataNascimento.setEnabled(false);
txtLogradouro.setEnabled(false);
txtComplemento.setEnabled(false);
txtBairro.setEnabled(false);
txtCidade.setEnabled(false);
txtCaixaPostal.setEnabled(false);
txtContato.setEnabled(false);
txtEmpresa.setEnabled(false);
txtEmail.setEnabled(false);
txtSite.setEnabled(false);
txtUf.setEnabled(false);
txtCEP.setEnabled(false);
txtFoneResidencial.setEnabled(false);
txtFoneComercial.setEnabled(false);
txtCelular1.setEnabled(false);
txtCelular2.setEnabled(false);
txtFax.setEnabled(false);
txtCnpj.setEnabled(false);
txtCpf.setEnabled(false);
txtRg.setEnabled(false);
txtIe.setEnabled(false);
txtrObservacao.setEnabled(false);
}
public void clearFields() {
txtId.setText(null);
txtNome.setText(null);
txtDataNascimento.setText(null);
txtLogradouro.setText(null);
txtComplemento.setText(null);
txtBairro.setText(null);
txtCidade.setText(null);
txtCaixaPostal.setText(null);
txtContato.setText(null);
txtEmpresa.setText(null);
txtEmail.setText(null);
txtSite.setText(null);
txtUf.setText(null);
txtCEP.setText(null);
txtFoneResidencial.setText(null);
txtFoneComercial.setText(null);
txtCelular1.setText(null);
txtCelular2.setText(null);
txtFax.setText(null);
txtCnpj.setText(null);
txtCpf.setText(null);
txtRg.setText(null);
txtIe.setText(null);
txtrObservacao.setText(null);
}
}