MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;

public class MainClass {
    public static void main(String args[]) {
        JFrame frame = new JFrame("Range Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(3, 2));

        frame.add(new JLabel("Range: 0-255"));
        JTextField textFieldOne = new JTextField();
        Document textDocOne = textFieldOne.getDocument();
        DocumentFilter filterOne = new IntegerRangeDocumentFilter(0, 255);
        ((AbstractDocument) textDocOne).setDocumentFilter(filterOne);
        frame.add(textFieldOne);

        frame.setSize(250, 150);
        frame.setVisible(true);
    }
}

class IntegerRangeDocumentFilter extends DocumentFilter {

    int minimum, maximum;

    int currentValue = 0;

    public IntegerRangeDocumentFilter(int minimum, int maximum) {
        this.minimum = minimum;
        this.maximum = maximum;
    }

    public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr)
            throws BadLocationException {

        if (string == null) {
            return;
        } else {
            String newValue;
            Document doc = fb.getDocument();
            int length = doc.getLength();
            if (length == 0) {
                newValue = string;
            } else {
                String currentContent = doc.getText(0, length);
                StringBuffer currentBuffer = new StringBuffer(currentContent);
                currentBuffer.insert(offset, string);
                newValue = currentBuffer.toString();
            }
            currentValue = checkInput(newValue, offset);
            fb.insertString(offset, string, attr);
        }
    }

    public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {

        Document doc = fb.getDocument();
        int currentLength = doc.getLength();
        String currentContent = doc.getText(0, currentLength);
        String before = currentContent.substring(0, offset);
        String after = currentContent.substring(length + offset, currentLength);
        String newValue = before + after;
        currentValue = checkInput(newValue, offset);
        fb.remove(offset, length);
    }

    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
            throws BadLocationException {

        Document doc = fb.getDocument();
        int currentLength = doc.getLength();
        String currentContent = doc.getText(0, currentLength);
        String before = currentContent.substring(0, offset);
        String after = currentContent.substring(length + offset, currentLength);
        String newValue = before + (text == null ? "" : text) + after;
        currentValue = checkInput(newValue, offset);
        fb.replace(offset, length, text, attrs);
    }

    private int checkInput(String proposedValue, int offset) throws BadLocationException {
        int newValue = 0;
        if (proposedValue.length() > 0) {
            try {
                newValue = Integer.parseInt(proposedValue);
            } catch (NumberFormatException e) {
                throw new BadLocationException(proposedValue, offset);
            }
        }
        if ((minimum <= newValue) && (newValue <= maximum)) {
            return newValue;
        } else {
            throw new BadLocationException(proposedValue, offset);
        }
    }
}