Testa Entrada de Dados no JtextField com Regex

0 respostas
jorgereidinaldo

Galera estou com um problema tenho meu textField funcionando corretamente em meu frame, porem agora quero fazer o teste da entrada de dados com o regex.
Criei um frame diretamente na mao e o teste funcionou corretamente agora estou trabalhando com o Eclipse e com WindowBuilder e não estou conseguindo fazer este teste.

:!: :!: gostaria que verificassem o que ha de errado em meu codigo
grato.

public class JfCliente extends JFrame {

	private JPanel contentPane;
	private JTextField tfCpf;
	

	/**
	 * Jorge Reidinaldo
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JfCliente frame = new JfCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public JfCliente() throws ParseException {
		setTitle("Cadastro Cliente");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1280, 800);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		
		
		
		tfCpf= new JTextField(new CustomPlainDocument(), "", 20);
		tfCpf.setBounds(988, 22, 174, 18);
		panel.add(tfCpf);
		tfCpf.setColumns(10);
		
		
		
	}
}
public class CustomPlainDocument extends PlainDocument {
	public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
	    String currentText = getText(0, getLength());
	    String beforeOffset = currentText.substring(0, offs);
   // String padrao= "[\\s{2,}";
	    String afterOffset = currentText.substring(offs, currentText.length());
	    String proposedResult = beforeOffset + str + afterOffset;
	    
	    Pattern pattern = Pattern.compile("[0-9]+");
	    
	        
	    Matcher matcher = pattern.matcher(proposedResult);
	 
	    if (matcher.matches()) {
	      super.insertString(offs, str, a);
	    }
	  }

}
Criado 18 de setembro de 2011
Respostas 0
Participantes 1