Frage

Just wanted to know why this is occurring, not that it's a huge issue.


Problem

I have a class, JDecimalField, that extends JTextField. I've modified the createDefaultModel() code in this class so it returns a custom Document that writes only numerical inputs (1 through 9 as well as '.').

Created a quick SSCCE of the code I have, excuse the dirtiness:

import java.io.Serializable;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class TestField{
    
    public static void main(String args[]){
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JDecimalField field = new JDecimalField();
        frame.add(field);
        frame.pack();
        frame.setVisible(true);          
    }
    
    public static class JDecimalField extends JTextField{

        public JDecimalField() {
            super();
        }

        @Override
        protected Document createDefaultModel() {
            String[] inputs = {"1", "2", "3", "4","5","6","7","8","9", "0", "."};
            return new RestrictedDocument(inputs); 
        }

        public int getValue() throws NumberFormatException{
            return Integer.parseInt(getText());
        }  
    }
    
    public static class RestrictedDocument extends PlainDocument implements Serializable {
        private final String[] charList;

        public RestrictedDocument(String[] charList){
            this.charList = charList;
        }

        public boolean validate(String exp){
            for (String charList1 : charList) {
                if (exp.equals(charList1)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            if (validate(str)){
                super.insertString(offs, str, a);
            }     
        }       
    }
    
}

My Question/TL;DR:

I just was wondering what in my RestrictedDocument is not accepting a ctrl+v action? Being able to do so would be nice but I could live without it.

And before it's mentioned, yes I've considered using a JFormattedTextField. I usually do, just in my case I can't for this current assignment. I don't mind though, expounds upon my knowledge of Java.

War es hilfreich?

Lösung

The solution is simple:

  • Either use a JFormattedTextField (as you already know) or
  • use a DocumentFilter.

For example:
DocumentFilter Example 1
DocumentFilter Example 2

For tutorial:
DocumentFilter Tutorial


Edit
Your problem is in your validate method: it will allow "3" since that's in the list, but won't allow "33" since that's not in the list.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top