如何使用占位符字符设置JFormattedTextField的值? [英] How do I set the value of a JFormattedTextField with a placeholder character?

查看:79
本文介绍了如何使用占位符字符设置JFormattedTextField的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

推荐答案

相当奇怪,但这出现在另一个问题中(Java:网络设置窗口)。经过挖掘后,Sun推出了RegexFormatter实现(参见 http:/ /java.sun.com/products/jfc/tsc/articles/reftf/ ;在 http://java.sun.com/products/jfc/tsc/articles/reftf/RegexFormatter.java )你可以像这样使用:

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的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆