Link to home
Start Free TrialLog in
Avatar of incah
incah

asked on

Better JTextField

How can I create a JTextField that accepts only a predefined length of characters, or accepts only numbers?
ASKER CERTIFIED SOLUTION
Avatar of gadio
gadio

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of incah
incah

ASKER

You mean a DocumentListener. There is no TextListener for a JTextField. Anyway I did it in the following code,but it doesn't work as expected. When the first non-digit character is entered the program that uses the NumberJTextField freezes. But even if it worked I can't find of a nice way to change the content of the field (see method doWork() which removes the entire contents and inserts a new String. There I'm forced to pass null as the third parameter to insertString() because I don't know what else to pass).

import com.sun.java.swing.*;
import com.sun.java.swing.text.*;
import com.sun.java.swing.event.*;

public class NumberJTextField extends JTextField {
      public NumberJTextField() {
            super();
            this.getDocument().addDocumentListener(new MyDocumentAdapter());
      }
      public NumberJTextField(String text) {
            super(checkedString(text));
            this.getDocument().addDocumentListener(new MyDocumentAdapter());
      }
      public NumberJTextField(int columns) {
            super(columns);
            this.getDocument().addDocumentListener(new MyDocumentAdapter());
      }
      public NumberJTextField(String text,int columns) {
            super(checkedString(text),columns);
            this.getDocument().addDocumentListener(new MyDocumentAdapter());
      }
      public NumberJTextField(Document doc,String text,int columns) {
            super(doc,checkedString(text),columns);
            this.getDocument().addDocumentListener(new MyDocumentAdapter());
      }
      private static String checkedString(String text) {
            if (text==null || text.equals(""))
                  return(text);
            int len=text.length();
            StringBuffer out=new StringBuffer(len);
            char c;
            for (int i=0;i<len;i++) {
                  c=text.charAt(i);
                  if (c>='0' && c<='9')
                        out.append(c);
            }
            return(out.toString());
      }
      public void setText(String text) {
            super.setText(checkedString(text));
      }

      class MyDocumentAdapter implements DocumentListener {
            public void insertUpdate(DocumentEvent e) {
                  doWork(e);
            }
            public void removeUpdate(DocumentEvent e) {
            }
            public void changedUpdate(DocumentEvent e) {
                  doWork(e);
            }
            private void doWork(DocumentEvent e) {
                  Document doc=e.getDocument();
                  int len=doc.getLength();
                  if (len>0) {
                        try {
                              String text=doc.getText(0,len),
                                                                               out=checkedString(text);
                              if (!text.equals(out)) {
                                    doc.remove(0,len);
                                    doc.insertString(0,out,null);
                              }
                        } catch(BadLocationException ble) {
                        }

                  }
            }
      }

}