Typing Arabic numbers in JTextField

前端 未结 1 1261
心在旅途
心在旅途 2021-01-24 13:38

I am trying to type Arabic numbers in a JTextField and I used DocumentListener as follows:

txtName.getDocument().addDocumentListener(th         


        
相关标签:
1条回答
  • 2021-01-24 14:14

    If you read the DocumentListener API, you'll see why this error occurred:

    The DocumentEvent notification is based upon the JavaBeans event model. There is no guarantee about the order of delivery to listeners, and all listeners must be notified prior to making further mutations to the Document. This means implementations of the DocumentListener may not mutate the source of the event (i.e. the associated Document).

    Consider using a DocumentFilter instead.

    e.g.,

    import javax.swing.*;
    import javax.swing.text.AttributeSet;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.DocumentFilter;
    import javax.swing.text.PlainDocument;
    
    @SuppressWarnings("serial")
    public class DocListenerProblem extends JPanel {
       private static final String REPLACE_CHARS = "0123456789.";
       private JTextField txtName = new JTextField(20);
    
       public DocListenerProblem() {
          add(txtName);
          PlainDocument doc = (PlainDocument) txtName.getDocument();
          doc.setDocumentFilter(new MyDocumentFilter());
       }
    
       private class MyDocumentFilter extends DocumentFilter {
    
          @Override
          public void insertString(FilterBypass fb, int offset, String text,
                   AttributeSet attr) throws BadLocationException {
             if (REPLACE_CHARS.contains(text)) {
                text = doSwap(text);
             }
             super.insertString(fb, offset, text, attr);
          }
    
          @Override
          public void replace(FilterBypass fb, int offset, int length, String text,
                   AttributeSet attrs) throws BadLocationException {
             if (REPLACE_CHARS.contains(text)) {
                text = doSwap(text);
             }
             super.replace(fb, offset, length, text, attrs);
          }
    
          @Override
          public void remove(FilterBypass fb, int offset, int length)
                   throws BadLocationException {
             super.remove(fb, offset, length);
          }
       }
    
       public String doSwap(String text) {
          StringBuilder sb = new StringBuilder();
          for (char c : text.toCharArray()) {
             if (REPLACE_CHARS.contains(String.valueOf(c))) {
                if (c == '.') {
                   c = ',';
                } else {
                   c = (char) ('\u0660' - '0' + c);
                }
             }
             sb.append(c);
          }
          return sb.toString();
       }
    
       private static void createAndShowUI() {
    
          JFrame frame = new JFrame("DocListenerProblem");
          frame.getContentPane().add(new DocListenerProblem());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    
    }
    
    0 讨论(0)
提交回复
热议问题