5

I have been trying to custom JFormattedTextField to display a mask for entering an IP address.

I read javadoc, and I tried with ###.###.###.###, but it's not valid in many cases.

I found some article for this, they use Regex to format value but they don't display a mask for entering an IP.

I want to display 3 dots in text field to display IP address. Like this:

 .   .   .

I can input a IP Address in many cases. You can see in IPv4 configuration in Windows.

 .   .   .

Can you help me? Thanks for reading

assylias
  • 321,522
  • 82
  • 660
  • 783
Do Manh
  • 144
  • 2
  • 9
  • 3
    `I read javadoc, and I tried with ###.###.###.###, but it's not valid in many cases.` maybe simple mistake, for better help sooner post an [SSCCE](http://sscce.org/) demonstraded your issue with [InputMask and Formatter](http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html) – mKorbel Aug 24 '12 at 08:27
  • 2
    +1 for adding the [tag:homework] tag. – Duncan Jones Aug 24 '12 at 08:29
  • Most likely `MaskFormatter` isn't flexible enough to give satisfactory behavior. Do you _need_ to use `MaskFormatter` or was it just what you happened to try? – Jacob Raihle Aug 24 '12 at 13:03

2 Answers2

2

I know this won't help you with your homework, but for other people looking for a solution: Jidesoft Grids (I am not affiliated) has a class for this: com.jidesoft.field.IPTextField.

Generally, I think it will be difficult to achieve a satisfactory solution with JFormattedTextField.

Not a bug
  • 4,286
  • 2
  • 40
  • 80
Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
2

You could try four JFormattedTextField in a row, each separated by a JLabel("."). With the dot out of the way, that might make the number format easier to get right.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42