I'm looking for a way to program textfields with dots like the one from Windows network settings dialog:
see >> http://i.stack.imgur.com/gayeY.jpg
Is there any开发者_开发问答 ready-to-use example on the net? - unfortunately I didn't find anything.
Thanks a lot for your help!
-patrick
As previously discussed on Stack Overflow (see How do I set the value of a JFormattedTextField with a placeholder character?), you can't easily use the JFormattedTextField to input IP addresses. However, there is also the RegexFormatter from Sun (see http://java.sun.com/products/jfc/tsc/articles/reftf/; download the source code at http://java.sun.com/products/jfc/tsc/articles/reftf/RegexFormatter.java) which you can use like this:
JFormattedTextField ipAddress;
try{
RegexFormatter ipmask = new RegexFormatter("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}");
ipmask.setOverwriteMode(false);
ipAddress = new JFormattedTextField(ipmask);
}catch(Exception e1){
}
ipAddress.setValue("255.255.255.255");
This will let you enter/edit the value and preserve the dots in the output.
With the help of a simple google search i found JFormattedTextField, here's an example on how to use it.
IP-address example:
public static void main(String args[]) throws ParseException
{
JFrame frame = new JFrame("Test");
JTextField f = new JFormattedTextField(new MaskFormatter("###.###.###.###"));
f.setFont(new Font("Monospaced", Font.PLAIN, 10));
frame.add(f);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 50);
frame.setVisible(true);
}
精彩评论