Java JTextComponent maxLength(JTextComponent textComponent, int length)

Here you can find the source of maxLength(JTextComponent textComponent, int length)

Description

max Length

License

Open Source License

Declaration

public static KeyListener maxLength(JTextComponent textComponent, int length) 

Method Source Code


//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/*from w w w .  j  av a 2s .  c  om*/
            public void keyTyped(KeyEvent e) {
                if (textComponent.getText().length() >= length) {
                    e.consume();
                    textComponent.setText(textComponent.getText().substring(0, length));
                }
            }
        };
    }
}

Related

  1. isJavaContext(final JTextComponent component, final int offset, final boolean allowInStrings)
  2. isNonWhitespaceBetween(JTextComponent editor, int iStart, int iEnd)
  3. loadFileToPane(String fname, JTextComponent pane)
  4. loadTextToPane(String text, JTextComponent pane, boolean append)
  5. makeVisible(JTextComponent textPane, int start, int end)
  6. paintCurrentLineBackground(final Graphics g, final JTextComponent c, final Color col)
  7. paintUnderline(Graphics g, JTextComponent tc, int pos0, int pos1)
  8. parseDouble(JTextComponent textComponent)
  9. readOnly(JTextComponent c, Component parent)