get swing Positive Digital Filter Document - Java Swing

Java examples for Swing:JTextPane

Description

get swing Positive Digital Filter Document

Demo Code


//package com.java2s;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

import javax.swing.text.PlainDocument;

public class Main {
    public static PlainDocument getPositiveDigitalFilterDocument() {
        PlainDocument digitalFilterDocument = new PlainDocument() {
            @Override/*www  .j a va2 s .  c  o  m*/
            public void insertString(int offs, String str, AttributeSet a)
                    throws BadLocationException {
                String targetStr = super.getText(0, offs) + str
                        + super.getText(offs, super.getLength() - offs);
                if (targetStr.length() > 0
                        && targetStr
                                .matches("^\\d+((\\.\\d+%?$)|(\\d*%?$))")) {
                    super.insertString(offs, str, a);
                }
            }
        };
        return digitalFilterDocument;
    }
}

Related Tutorials