[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Better JTextField

How can I create a JTextField that accepts only a predefined length of characters, or accepts only numbers?
0
incah
Asked:
incah
1 Solution
 
gadioCommented:
incah, add a TextListener to the JTextField, that on every text change event will check the contents of the field. If the contents is not valid, you can undo the last character or beep or whatever. Note that you can use the java.text.NumberFormat to help you with the nunmbers.
0
 
incahAuthor Commented:
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) {
                        }

                  }
            }
      }

}

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now