0

I've been searching and I can't find a place where I understand how to create a JFormattedTextField that only allows integers as input. Is it even possible?

edit found this code from @HovercraftFullOfEels:

public class QuantFilter extends DocumentFilter {
    @Override
    public void insertString(FilterBypass fb, int offset, String string,
             AttributeSet attr) throws BadLocationException {

          Document doc = fb.getDocument();
          StringBuilder sb = new StringBuilder();
          sb.append(doc.getText(0, doc.getLength()));
          sb.insert(offset, string);

          if (test(sb.toString())) {
             super.insertString(fb, offset, string, (javax.swing.text.AttributeSet) attr);
          }
       }

       private boolean test(String text) {
          try {
             Integer.parseInt(text);
             return true;
          } catch (NumberFormatException e) {
             return false;
          }
       }
       @Override
       public void replace(FilterBypass fb, int offset, int length, String text,
             AttributeSet attrs) throws BadLocationException {

          Document doc = fb.getDocument();
          StringBuilder sb = new StringBuilder();
          sb.append(doc.getText(0, doc.getLength()));
          sb.replace(offset, offset + length, text);

          if (test(sb.toString())) {
             super.replace(fb, offset, length, text, (javax.swing.text.AttributeSet) attrs);
          }
       }

       @Override
       public void remove(FilterBypass fb, int offset, int length)
             throws BadLocationException {
          Document doc = fb.getDocument();
          StringBuilder sb = new StringBuilder();
          sb.append(doc.getText(0, doc.getLength()));
          sb.delete(offset, offset + length);

          if (test(sb.toString())) {
             super.remove(fb, offset, length);
          }
       }
}

The overrides say that it should override a method, and if I take it doesn't enter the insertString()

Optional
  • 4,387
  • 4
  • 27
  • 45
CarlosMorgado
  • 315
  • 2
  • 10
  • possible duplicate: http://stackoverflow.com/questions/14467858/how-to-make-jformattedtextfield-accept-integer-without-decimal-point-comma, http://stackoverflow.com/questions/8637792/how-to-set-jformattedtextfield-so-it-only-allows-2-numbers, http://stackoverflow.com/questions/5662651/how-to-implement-in-java-jtextfield-class-to-allow-entering-only-digits. Have you really been searching? – cello Dec 03 '14 at 21:29
  • yeah but from what I understand they let the none Intiger in, I meant something like the JTable when you try to insert a none Intiger value in an Intege cell it won't let it – CarlosMorgado Dec 03 '14 at 21:37
  • 1
    Myself, I'd use a DocumentFilter as per [my answer and example program](http://stackoverflow.com/a/11093360/522444), and if you need to do it for a JTable, then implement it in your table's cell editor. The JTable tutorial will show you how to use table cell editors. That's really all the information needed to solve this. – Hovercraft Full Of Eels Dec 03 '14 at 21:43
  • @HovercraftFullOfEels your code doesn't allow to delete? and from what I tried I can insert letter, maybe I did something wrong – CarlosMorgado Dec 03 '14 at 21:54
  • @CarlosMorgado: the code will allow what you let it allow, no more, no less. If you're having trouble with new code of yours, post it here and let's see. – Hovercraft Full Of Eels Dec 03 '14 at 22:01
  • actually just found this problem wrong import – CarlosMorgado Dec 03 '14 at 22:13

0 Answers0