Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.text.JTextComponent;

public class Main {
    public static KeyListener maxLength(JTextComponent textComponent, int length) {
        return new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (textComponent.getText().length() >= length) {
                    e.consume();
                    textComponent.setText(textComponent.getText().substring(0, length));
                }
            }
        };
    }
}