问题
here is an exemple of code :
private JFormattedTextField jftf2 = new JFormattedTextField();
try{
MaskFormatter mask = new MaskFormatter("###-####");
mask.install(jftf2);
}catch(ParseException e){e.printStackTrace();}
when I execute it, it shows 3 spaces then '-' then 4 spaces,i want to know how not to have those spaces, a simple empty textfield that receives no more than the characters i write. i know that there is a method mask.setPlaceholderCharacter(' ');
that changes the space to another character but I just want to delete it, not to change it.
another problem is while changing the text, for exemple let's take the same mask ###-####
the string 111-1234
is valid in that mask but when i delete the 3 for exemple it shows 111-12 4
instead of 111-124
, it adds a space, how to resolve that ?
thanx
回答1:
It’s not quite clear what you want as it is a bit strange to use a MaskFormatter
and complain that it behaves like a MaskFormatter
. But maybe you wish to do something like this:
JFormattedTextField jftf2 = new JFormattedTextField();
final InternationalFormatter fmt=new InternationalFormatter(
new MessageFormat("{0,number,000}-{1,number,0000}"));
jftf2.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
public JFormattedTextField.AbstractFormatter
getFormatter(JFormattedTextField tf) {
return fmt;
}
});
jftf2.setValue(new Object[]{111,1234});
With this format the value
property of the JFormattedTextField
will be expressed as an object array of length 2
containing two numbers. The 000
and 0000
in the format specifier above tell it to format the numbers with leading zeros by default (though the user does not need to input them to make a valid input). But you can change it to 0
for compact numbers.
来源:https://stackoverflow.com/questions/20006865/how-to-delete-spaces-in-a-jformattedtextfield