[quote=adriano.braga83]Amigo eu faço assim:
javax.swing.text.MaskFormatter maskcgc = null;
try{
maskcgc = new javax.swing.text.MaskFormatter("##.###.###/####-##");
}
catch (java.text.ParseException exc){}
jFormattedTextField2 = new javax.swing.JFormattedTextField(maskcgc);
faz o teste desse jeito e dica se consegui ok?
Abraço![/quote]
amigo não deu certo aqui não…
segue meu código inteiro para quem puder me ajudar… o problema é que além de não ficar com a máscara, ele tira o amarelo da cor de fundo quando o foco tá no textField…
[code]package lista02;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.text.MaskFormatter;
public class Janela extends javax.swing.JFrame {
public Janela() {
initComponents();
centralizar();
setResizable(false);
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent evt) {
JOptionPane.showMessageDialog(null,
"Universidade Presbiteriana Mackenzie!",
"Linguagem de Programação II",
JOptionPane.INFORMATION_MESSAGE);
}
@Override
public void windowClosing(WindowEvent evt) {
JOptionPane.showMessageDialog(null, "Obrigado por utilizar nosso sistema!");
}
});
jbConsultar.addActionListener (new ActionListener (){
@Override
public void actionPerformed (ActionEvent evt){
if ((jtfAgencia.getText().isEmpty() == true) || (jtfConta.getText().isEmpty() == true)){
JOptionPane.showMessageDialog(null, "É necessário informar a agência e a conta!");
}
}
});
jbFechar.addActionListener (new ActionListener (){
@Override
public void actionPerformed (ActionEvent evt){
JOptionPane.showMessageDialog(null, "Obrigado por utilizar nosso sistema!");
System.exit(0);
}
});
jtfAgencia.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfAgencia.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfAgencia.setBackground(Color.WHITE);
}
});
jtfConta.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfConta.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfConta.setBackground(Color.WHITE);
}
});
jtfNome.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfNome.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfNome.setBackground(Color.WHITE);
}
});
jtfEndereco.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfEndereco.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfEndereco.setBackground(Color.WHITE);
}
});
jtfTelefone.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfTelefone.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfTelefone.setBackground(Color.WHITE);
}
});
jtfCpf.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent evt) {
jtfCpf.setBackground(Color.YELLOW);
}
@Override
public void focusLost(FocusEvent evt) {
jtfCpf.setBackground(Color.WHITE);
}
});
/*try {
jtfAgencia =
new JFormattedTextField(
new MaskFormatter("####-#"));
} catch (ParseException ex) {
}
try {
jtfConta =
new JFormattedTextField(
new MaskFormatter("#####-##"));
} catch (ParseException ex) {
}
try {
jtfTelefone =
new JFormattedTextField(
new MaskFormatter("(0xx##) ####-####"));
} catch (ParseException ex) {
}
try {
jtfCpf =
new JFormattedTextField(
new MaskFormatter("###.###.###-##"));
} catch (ParseException ex) {
}*/
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
bgContas = new javax.swing.ButtonGroup();
jlAgencia = new javax.swing.JLabel();
jtfAgencia = new javax.swing.JTextField();
jlConta = new javax.swing.JLabel();
jtfConta = new javax.swing.JTextField();
jSeparator1 = new javax.swing.JSeparator();
jSeparator2 = new javax.swing.JSeparator();
jlNome = new javax.swing.JLabel();
jlEndereco = new javax.swing.JLabel();
jlTelefone = new javax.swing.JLabel();
jlCpf = new javax.swing.JLabel();
jrbCorrente = new javax.swing.JRadioButton();
jrbPoupanca = new javax.swing.JRadioButton();
jbConsultar = new javax.swing.JButton();
jbAtualizar = new javax.swing.JButton();
jbFechar = new javax.swing.JButton();
jtfNome = new javax.swing.JTextField();
jtfEndereco = new javax.swing.JTextField();
jtfTelefone = new javax.swing.JTextField();
jtfCpf = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Linguagem de Programação II");
setResizable(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jlAgencia.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlAgencia.setText("Código da Agência:");
getContentPane().add(jlAgencia, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 110, 18));
jtfAgencia.setToolTipText("Digite o código da agência.");
getContentPane().add(jtfAgencia, new org.netbeans.lib.awtextra.AbsoluteConstraints(125, 10, 50, -1));
jlConta.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlConta.setText("Número da Conta:");
getContentPane().add(jlConta, new org.netbeans.lib.awtextra.AbsoluteConstraints(205, 10, 105, 18));
jtfConta.setToolTipText("Digite o número da conta.");
getContentPane().add(jtfConta, new org.netbeans.lib.awtextra.AbsoluteConstraints(315, 10, 60, -1));
getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 365, 10));
getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, 365, 10));
jlNome.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlNome.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jlNome.setText("Nome:");
getContentPane().add(jlNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 60, 18));
jlEndereco.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlEndereco.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jlEndereco.setText("Endereço:");
getContentPane().add(jlEndereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 75, 60, 18));
jlTelefone.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlTelefone.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jlTelefone.setText("Telefone:");
getContentPane().add(jlTelefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, 60, 18));
jlCpf.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jlCpf.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
jlCpf.setText("CPF:");
getContentPane().add(jlCpf, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 125, 60, 18));
bgContas.add(jrbCorrente);
jrbCorrente.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jrbCorrente.setMnemonic('C');
jrbCorrente.setSelected(true);
jrbCorrente.setText("Conta Corrente");
getContentPane().add(jrbCorrente, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 150, -1, 20));
bgContas.add(jrbPoupanca);
jrbPoupanca.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jrbPoupanca.setMnemonic('P');
jrbPoupanca.setText("Conta Poupança");
getContentPane().add(jrbPoupanca, new org.netbeans.lib.awtextra.AbsoluteConstraints(225, 150, 118, 20));
jbConsultar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jbConsultar.setMnemonic('S');
jbConsultar.setText("Consultar");
jbConsultar.setToolTipText("Clique para consultar.");
getContentPane().add(jbConsultar, new org.netbeans.lib.awtextra.AbsoluteConstraints(35, 190, 100, -1));
jbAtualizar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jbAtualizar.setMnemonic('A');
jbAtualizar.setText("Atualizar");
jbAtualizar.setToolTipText("Clique para atualizar.");
jbAtualizar.setEnabled(false);
getContentPane().add(jbAtualizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(145, 190, 100, -1));
jbFechar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jbFechar.setMnemonic('F');
jbFechar.setText("Fechar");
jbFechar.setToolTipText("Clique para fechar.");
getContentPane().add(jbFechar, new org.netbeans.lib.awtextra.AbsoluteConstraints(255, 190, 100, -1));
jtfNome.setToolTipText("Digite o seu nome.");
getContentPane().add(jtfNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(75, 50, 300, 20));
jtfEndereco.setToolTipText("Digite o seu endereço.");
getContentPane().add(jtfEndereco, new org.netbeans.lib.awtextra.AbsoluteConstraints(75, 75, 300, 20));
jtfTelefone.setToolTipText("Digite o seu telefone.");
getContentPane().add(jtfTelefone, new org.netbeans.lib.awtextra.AbsoluteConstraints(75, 100, 300, 20));
jtfCpf.setToolTipText("Digite seu CPF.");
getContentPane().add(jtfCpf, new org.netbeans.lib.awtextra.AbsoluteConstraints(75, 125, 300, 20));
pack();
}// </editor-fold>
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Janela().setVisible(true);
}
});
}
private void centralizar() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension janela = getSize();
if (janela.height > screen.height)
setSize(janela.width, screen.height);
if (janela.width > screen.width)
setSize(screen.width, janela.height);
setLocation((screen.width - janela.width) / 2,(screen.height - janela.height) / 2);
}
// Variables declaration - do not modify
private javax.swing.ButtonGroup bgContas;
private javax.swing.JSeparator jSeparator1;
private javax.swing.JSeparator jSeparator2;
private javax.swing.JButton jbAtualizar;
private javax.swing.JButton jbConsultar;
private javax.swing.JButton jbFechar;
private javax.swing.JLabel jlAgencia;
private javax.swing.JLabel jlConta;
private javax.swing.JLabel jlCpf;
private javax.swing.JLabel jlEndereco;
private javax.swing.JLabel jlNome;
private javax.swing.JLabel jlTelefone;
private javax.swing.JRadioButton jrbCorrente;
private javax.swing.JRadioButton jrbPoupanca;
private javax.swing.JTextField jtfAgencia;
private javax.swing.JTextField jtfConta;
private javax.swing.JTextField jtfCpf;
private javax.swing.JTextField jtfEndereco;
private javax.swing.JTextField jtfNome;
private javax.swing.JTextField jtfTelefone;
// End of variables declaration
}
[/code]