问题描述
我有一个ip地址的格式化文本字段:
I have a formatted text field for ip address:
ipmask = new MaskFormatter("###.###.###.###");
ipmask.setPlaceholderCharacter(' ');
field = new JFormattedTextField(ipmask);
field.setValue(111.222.333.444);
有效,但
field.setValue(10.222.333.444);
不起作用
field.setValue(10 .222.333.444);
不起作用
field.setValue(10.222.333.444);
不起作用
什么是设置值的正确方法是什么?
What is the right way to set the value?
推荐答案
相当奇怪,但这出现在另一个问题中()。经过挖掘后,Sun推出了RegexFormatter实现(参见;在)你可以像这样使用:
Rather odd, but this came up in another question (at Java: network settings window). After digging around turns out there is a RegexFormatter implementation 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");
你可能已经离开这里了,但我想我会坚持这个以防万一有人别的徘徊。
You've probably moved on from here, but thought I'd stick this in just in case someone else wanders along.
这篇关于如何使用占位符字符设置JFormattedTextField的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!