Mascara em JTextField

2 respostas
E

como adicionar esse metodo no JTextField???

import javax.swing.JOptionPane;  
    import javax.swing.text.MaskFormatter;  
    
    public class Mascara {  
        private MaskFormatter cpf = new MaskFormatter();  
   
         
       public MaskFormatter getCpf(){  
           try{  
               this.cpf.setMask("###.###.###-##");  
               this.cpf.setValidCharacters("[telefone removido]");  
           } catch (Exception e){  
               JOptionPane.showMessageDialog(null,  
                       "Erro ao criar uma m�scara",  
                       "Mensagem do Sistema",  
                       JOptionPane.ERROR_MESSAGE);  
           }  
           return this.cpf;  
       }  
         
       }

como colocá-la aqui???????
ou chamá-la aqui??????

public RotuloClientes()
   {
      // Painel de Rótulo
      
      
      labelPanel = new JPanel();
      
      labelPanel.setLayout(new GridLayout ( labels.length,1 ) );

      for ( int i = 0; i < labels.length; i++ )
         labelPanel.add( new JLabel( labels[ i ], 0) );
             
      // Painel de TextField 
      fieldsPanel = new JPanel();
      fieldsPanel.setLayout(new GridLayout( labels.length, 1 ) );

      
      cod = new JTextField( 20 );
      cod.setEditable( false );
      fieldsPanel.add( cod );
      cod.setFont(new Font("Arial", Font.BOLD,24));
      cod.setForeground(Color.red);
      
      cpf = new JTextField( 9 );
      fieldsPanel.add(cpf);
         
      primeironome = new JTextField( 20 );
      fieldsPanel.add( primeironome );

2 Respostas

L

Segue um exemplo.

Espero que lhe ajude!

import java.awt.FlowLayout;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.text.MaskFormatter;

public class ExemploFormattedTextField extends JFrame {

	private JFormattedTextField cpf;
	private MaskFormatter cpfFormatter;
	
	
	public ExemploFormattedTextField() throws ParseException{

		cpfFormatter = new MaskFormatter("###.###.###-##");
			
		cpf = new JFormattedTextField(cpfFormatter);
		
		cpfFormatter.setValidCharacters("[telefone removido]");

		cpf.setColumns(14);

		add(cpf);

		setLayout(new FlowLayout());
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setSize(200, 100);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		try {
			ExemploFormattedTextField teste = new ExemploFormattedTextField();		
		}catch (ParseException e) {
			e.printStackTrace();
		}
	}
}
E

Obrigado.
valeu!!!

Criado 21 de abril de 2008
Ultima resposta 27 de abr. de 2008
Respostas 2
Participantes 2