Solved

Better JTextField

Posted on 1998-08-03
2
268 Views
Last Modified: 2008-02-26
How can I create a JTextField that accepts only a predefined length of characters, or accepts only numbers?
0
Comment
Question by:incah
2 Comments
 
LVL 6

Accepted Solution

by:
gadio earned 50 total points
ID: 1230611
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
 

Author Comment

by:incah
ID: 1230612
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now