1

I want to use the AsYouTypeFormatter from LibPhoneNumber (http://code.google.com/p/libphonenumber/) with a JTextField to format a phone number. However, I cannot use a document listener to change the text in the JTextField and an ActionListener will only work when the mouse is clicked. All the other questions I have seen involve using the AsYouTypeFormatter for Android, but I am making a desktop application.

phoneUtil = PhoneNumberUtil.getInstance();
formatter = phoneUtil.getAsYouTypeFormatter("US");

addPhoneF = new JTextField(20);
addPhoneF.getDocument().addDocumentListener(this);

    @Override
public void insertUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    String unformattedNumber = addPhoneF.getText();
    String formattedNumber = "";
    for (int i = 0; i<unformattedNumber.length() - 1; i++) {
        formattedNumber += formatter.inputDigit(unformattedNumber.charAt(i));
    }
    addPhoneF.setText(formattedNumber);
}   

@Override
public void removeUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    String unformattedNumber = addPhoneF.getText();
    String formattedNumber = "";
    for (int i = 0; i<unformattedNumber.length() - 1; i++) {
        formattedNumber += formatter.inputDigit(unformattedNumber.charAt(i));
    }
    addPhoneF.setText(formattedNumber);
}


@Override
public void changedUpdate(DocumentEvent e) {
    // TODO Auto-generated method stub
    String unformattedNumber = addPhoneF.getText();
    String formattedNumber = "";
    for (int i = 0; i<unformattedNumber.length() - 1; i++) {
        formattedNumber += formatter.inputDigit(unformattedNumber.charAt(i));
    }
    addPhoneF.setText(formattedNumber);
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Cass
  • 870
  • 8
  • 21
  • I can't figure out how to use the DocumentFilter. – Cass Feb 23 '13 at 03:25
  • Cass: there are plenty of examples of how to use DocumentFilters on this site, many written by me, and I suggest that you search this site and give them a look. – Hovercraft Full Of Eels Feb 23 '13 at 04:23
  • @HovercraftFullOfEels I have looked at those and created a subclass of DocumentFilter, but I still can't figure out how to use the AsYouTypeFormatter with it. – Cass Feb 23 '13 at 16:31
  • I don't have the AsYouTypeFormatter, so I can't test it myself, but again, the main advantage of a DocumentFilter, is that it can detect changes made to the Document *before* it has been written to the Document, and so would allow you to change or filter these changes, and *then* update the Document. It really sounds as if this is what you need. – Hovercraft Full Of Eels Feb 23 '13 at 16:38
  • Yes, for instance look [here](http://stackoverflow.com/a/9346426/522444). The String text method parameter will have the current pre-update text. – Hovercraft Full Of Eels Feb 23 '13 at 16:58

1 Answers1

4

Perhaps you want to use a DocumentFilter, not a DocumentListener. The former will allow the code to get attempted changes to the Document before they occur, which I believe is what you want.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373