简体   繁体   中英

How do I set the value of a JFormattedTextField with a placeholder character?

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?

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.

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

I've tried to use the mask formatter but its not good with our situation here so i've come up with this method using Regex and instant validation for the user input.

This code is generated using gui builder:

jFormattedTextField2 = new javax.swing.JFormattedTextField();
jFormattedTextField2.setHorizontalAlignment(jFormattedTextField2.CENTER);
jFormattedTextField2.addCaretListener(new javax.swing.event.CaretListener() {
    public void caretUpdate(javax.swing.event.CaretEvent evt) {
        jFormattedTextField2CaretUpdate(evt);
    }
});

Here on every field update the input will be validated using matcher:

private void jFormattedTextField2CaretUpdate(javax.swing.event.CaretEvent evt) {                                                 
        // validation happen here and the text is red if IP is invalid
        final String regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
        final Pattern pattern = Pattern.compile(regex);
        String ip = jFormattedTextField2.getText();
        Matcher m = pattern.matcher(ip);
        jFormattedTextField2.setForeground(Color.red);
        if (m.matches()) {
            jFormattedTextField2.setForeground(Color.black);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM