ÿþ<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META content="text/html; charset=unicode" http-equiv=Content-Type> <META content="MSHTML 5.00.2614.3500" name=GENERATOR> <STYLE></STYLE> </HEAD> <BODY bgColor=#ffffff> <DIV><FONT face=Arial size=2>Vc pode usar JFormattedTextField, segue abaixo um exemplo de uso:</FONT></DIV> <DIV><FONT face=Arial size=2>Luciano Coelho.</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>/*<BR># A number<BR>? A letter<BR>A A letter or number<BR>* Anything<BR>U A letter, with lowercase characters mapped to their uppercase equivalents<BR>L A letter, with uppercase characters mapped to their lowercase equivalents<BR>H A hexadecimal digit (A-F, a-f, 0-9)<BR>' Used to escape another mask character</FONT></DIV> <DIV><FONT face=Arial size=2>*/</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>import java.awt.*;<BR>import javax.swing.*;<BR>import javax.swing.text.*;<BR>import java.util.*;<BR>import java.text.*;</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>public class FormattedSample {<BR>&nbsp; public static void main (String args[]) throws ParseException {<BR>&nbsp;&nbsp;&nbsp; JFrame f = new JFrame("JFormattedTextField Sample");<BR>&nbsp;&nbsp;&nbsp; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<BR>&nbsp;&nbsp;&nbsp; Container content = f.getContentPane();<BR>&nbsp;&nbsp;&nbsp; content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; // Four-digit year, followed by month name and day of month,<BR>&nbsp;&nbsp;&nbsp; // each separated by two dashes (--)<BR>&nbsp;&nbsp;&nbsp; DateFormat format = new SimpleDateFormat("yyyy--MMMM--dd");<BR>&nbsp;&nbsp;&nbsp; DateFormatter df&nbsp; = new DateFormatter(format);<BR>&nbsp;&nbsp;&nbsp; JFormattedTextField ftf1 = new JFormattedTextField(df);<BR>&nbsp;&nbsp;&nbsp; ftf1.setValue(new Date());<BR>&nbsp;&nbsp;&nbsp; content.add(ftf1);</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; // US Social Security number<BR>&nbsp;&nbsp;&nbsp; MaskFormatter mf1 = new MaskFormatter("###-##-####");<BR>&nbsp;&nbsp;&nbsp; mf1.setPlaceholderCharacter('_');<BR>&nbsp;&nbsp;&nbsp; JFormattedTextField ftf2 = new JFormattedTextField(mf1);<BR>&nbsp;&nbsp;&nbsp; content.add(ftf2);</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; // US telephone number<BR>&nbsp;&nbsp;&nbsp; MaskFormatter mf2 = new MaskFormatter("(###) ###-####");<BR>&nbsp;&nbsp;&nbsp; JFormattedTextField ftf3 = new JFormattedTextField(mf2);<BR>&nbsp;&nbsp;&nbsp; content.add(ftf3);</FONT></DIV> <DIV>&nbsp;</DIV> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; f.setSize(300, 100);<BR>&nbsp;&nbsp;&nbsp; f.show();<BR>&nbsp; }<BR>}<BR></FONT></DIV></BODY></HTML>

Responder a