Swing
Wie kann ich ein Textfeld erstellen, indem nur eine begrenzte Anzahl von Zeichen eingebbar sind?
package de.bastie.howto.swing.text;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LengthLimitedDocument extends PlainDocument{
private int maxSpace = 1024;
public LengthLimitedDocument (final int maxSpace) {
super ();
this.maxSpace = maxSpace;
}
public void insertString (final int offset,
final String text,
final AttributeSet attributeSet)
throws BadLocationException {
if (this.isEnoughSpace(text)){
super.insertString (offset, text, attributeSet);
}
}
protected boolean isEnoughSpace (final String toTest) {
if (this.getLength() + toTest.length() > this.maxSpace) {
return false;
}
return true;
}
}
all rights reserved © Bastie - Sebastian Ritter @:
w³: http://www.Bastie.de
Diese Seite ist Bestandteil der
Internetpräsenz unter http://www.Bastie.de
Java
Cobol
Software
Resourcen
Service
Links
Über mich
Zum Gästebuch
Forum