// Main.java package javaapplication; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.ParseException; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JOptionPane; public class Main { public Main() { JFrame frame = new JFrame(); final RegexFormatter rf = new RegexFormatter("(\\w)?flavio(\\w)?"); //responsável pela validação do texto. final JFormattedTextField tf = new JFormattedTextField(rf); frame.setLayout(new GridLayout(2, 1)); frame.add(tf); JButton b = new JButton("Testar..."); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { JOptionPane.showMessageDialog(null, rf.stringToValue(tf.getText())); } catch (ParseException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); } } }); frame.add(b); frame.setSize(new Dimension(300, 100)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new Main(); } }