Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.swing.JTextField;

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

public class Main {
    public static JTextField forceUpperCase(JTextField textField) {
        ((AbstractDocument) textField.getDocument()).setDocumentFilter(getUpperCaseFilter());
        return textField;
    }

    public static DocumentFilter getUpperCaseFilter() {
        return new DocumentFilter() {
            public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr)
                    throws BadLocationException {
                fb.insertString(offset, text.toUpperCase(), attr);
            }

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

                fb.replace(offset, length, text.toUpperCase(), attrs);
            }
        };
    }
}