get Swing Digital Filter Document - Java Swing

Java examples for Swing:JTextPane

Description

get Swing 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 getDigitalFilterDocument() {
        PlainDocument digitalFilterDocument = new PlainDocument() {

            @Override/*from  w  w  w.j ava2  s  . co  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