Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

public class Main {
    public static void main(String[] argv) {
        JTextComponent textComp = new JTextField();
        textComp.setDocument(new FixedSizePlainDocument(10));
    }
}

class FixedSizePlainDocument extends PlainDocument {
    int maxSize;

    public FixedSizePlainDocument(int limit) {
        maxSize = limit;
    }

    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        if ((getLength() + str.length()) <= maxSize) {
            super.insertString(offs, str, a);
        } else {
            throw new BadLocationException("Insertion exceeds max size of document", offs);
        }
    }
}