tags:

views:

250

answers:

1

I have a formatted text field for ip address:

ipmask = new MaskFormatter("###.###.###.###");
ipmask.setPlaceholderCharacter(' ');
field = new JFormattedTextField(ipmask);

field.setValue("111.222.333.444"); works but

field.setValue(" 10.222.333.444"); does not work

field.setValue("10 .222.333.444"); does not work

field.setValue("10.222.333.444"); does not work

What is the right way to set the value?

+1  A: 

spaces don't count as numbers (#) and the . count as anything. unfortunately you wont be able to match an IP address with the MaskFormatter unless you can find a way to have multiple MaskFormatters for 1 JFormattedTextField.

simpler

if (field.getValue().matches("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}")) //do something

EDIT: you'll have to use a regular JTextField and validate it

Rickster
I know I can write my own class to do this but it will be nicer to have built in java classes to do it.
5YrsLaterDBA