| Home >> All |
Source code: Interface/LimitedStyledDocument.java
1 package Interface; 2 3 /* 4 * LimitedStyledDocument.java 5 * 6 * Created on 17 August 2002, 15:44 7 */ 8 9 /* 10 * Adapted from K.Walrath, M.Campione, "The JFC Swing Tutorial", 11 * Addison-Wesley Longman Inc., 1999 12 * Page 675-676 13 */ 14 15 import javax.swing.*; 16 import javax.swing.text.*; 17 import java.awt.Toolkit; 18 19 public class LimitedStyledDocument extends DefaultStyledDocument{ 20 21 int maxchars; 22 23 /** Creates new ReadingFiles */ 24 public LimitedStyledDocument(int maxchar) { 25 maxchars = maxchar; 26 } 27 28 /* Rejects the entire insertion of text if contents too long */ 29 public void insertString(int offs, String str, AttributeSet a) throws 30 BadLocationException 31 { 32 if ((getLength() + str.length()) <= maxchars) 33 super.insertString(offs, str, a); 34 else 35 Toolkit.getDefaultToolkit().beep(); 36 } 37 }