com.ibm.as400.ui.framework.java
Class TextDocument

java.lang.Object
  extended by javax.swing.text.AbstractDocument
      extended by javax.swing.text.PlainDocument
          extended by com.ibm.as400.ui.framework.java.TextDocument
All Implemented Interfaces:
Serializable, Document

public class TextDocument
extends PlainDocument

A replacement document for JTextComponent and JComboBox objects which limits the maximum number of characters allowed as input by the user.

When a TextDocument has been assigned to a JTextComponent or JComboBox, the control will not allow input past the specified number of characters. The only exception are items in the JComboBox drop down. These can still be selected or typed regardless of their length.

The following code creates a TextDocument to restrict the length of a JTextComponent to 12.


 // Get the text component from the panel manager
 JTextComponent tc = (JTextComponent)m_panel.getComponent("mytextfield");
  
 // Assign a new TextDocument to restrict length to 12.
 tc.setDocument(new TextDocument(12));
 

The following code creates a TextDocument to restrict the length of a JComboBox to 10.


 // Get the combo box from the panel manager
 JComboBox cb = (JComboBox)m_panel.getComponent("mycombobox");
  
 // Get the internal text component and assign a new TextDocument
 JTextComponent tc = (JTextComponent)cb.getEditor().getEditorComponent();
 tc.setDocument(new TextDocument(10, cb));
 

Since:
v5r1m0
Version:
1.0, 10/12/99
Author:
B. Wood
See Also:
Serialized Form

Nested Class Summary
Modifier and Type Class and Description
 
Nested classes/interfaces inherited from class javax.swing.text.AbstractDocument
AbstractDocument.AbstractElement, AbstractDocument.AttributeContext, AbstractDocument.BranchElement, AbstractDocument.Content, AbstractDocument.DefaultDocumentEvent, AbstractDocument.ElementEdit, AbstractDocument.LeafElement
 
Field Summary
Modifier and Type Field and Description
 
Fields inherited from class javax.swing.text.PlainDocument
lineLimitAttribute, tabSizeAttribute
 
Fields inherited from class javax.swing.text.AbstractDocument
BAD_LOCATION, BidiElementName, ContentElementName, ElementNameAttribute, listenerList, ParagraphElementName, SectionElementName
 
Fields inherited from interface javax.swing.text.Document
StreamDescriptionProperty, TitleProperty
 
Constructor Summary
Constructor and Description
TextDocument(int maxLength)
          Constructs a document which will only accept user input less than maxLength characters.
TextDocument(int maxLength, JComboBox comboBox)
          Constructs a document for a JComboBox which will only accept user input less than maxLength characters.
TextDocument(int maxLength, JTextComponent textComponent)
          Constructs a document for a JTextComponent which will only accept user input less than maxLength characters.
 
Method Summary
Modifier and Type Method and Description
 int getMaxLength()
          Returns the maximum length allowed by this document.
 void insertString(int offset, String str, AttributeSet attr)
          Inserts a string into the document.
 void setMaxLength(int maxLength)
          Sets the maximum length allowed by this document.
 
Methods inherited from class javax.swing.text.PlainDocument
createDefaultRoot, getDefaultRootElement, getParagraphElement, insertUpdate, removeUpdate
 
Methods inherited from class javax.swing.text.AbstractDocument
addDocumentListener, addUndoableEditListener, createBranchElement, createLeafElement, createPosition, dump, fireChangedUpdate, fireInsertUpdate, fireRemoveUpdate, fireUndoableEditUpdate, getAsynchronousLoadPriority, getAttributeContext, getBidiRootElement, getContent, getCurrentWriter, getDocumentFilter, getDocumentListeners, getDocumentProperties, getEndPosition, getLength, getListeners, getProperty, getRootElements, getStartPosition, getText, getText, getUndoableEditListeners, postRemoveUpdate, putProperty, readLock, readUnlock, remove, removeDocumentListener, removeUndoableEditListener, render, replace, setAsynchronousLoadPriority, setDocumentFilter, setDocumentProperties, writeLock, writeUnlock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TextDocument

public TextDocument(int maxLength)
Constructs a document which will only accept user input less than maxLength characters.

Parameters:
maxLength - The maximum length of input allowed by the user. A value of 0 does not restrict input.
Since:
v5r1m0

TextDocument

public TextDocument(int maxLength,
                    JComboBox comboBox)
Constructs a document for a JComboBox which will only accept user input less than maxLength characters. The JComboBox to be restricted must be passed.

Parameters:
maxLength - The maximum length of input allowed by the user. A value of 0 does not restrict input.
comboBox - The JComboBox control to be restricted.
Since:
v5r1m0

TextDocument

public TextDocument(int maxLength,
                    JTextComponent textComponent)
Constructs a document for a JTextComponent which will only accept user input less than maxLength characters. The JTextComponent to be restricted must be passed.

Parameters:
maxLength - The maximum length of input allowed by the user. A value of 0 does not restrict input.
textComponent - The JTextComponent control to be restricted.
Since:
v5r1m0
Method Detail

insertString

public void insertString(int offset,
                         String str,
                         AttributeSet attr)
                  throws BadLocationException
Inserts a string into the document.

Specified by:
insertString in interface Document
Overrides:
insertString in class PlainDocument
Parameters:
offset - The offset into the document to insert the content.
str - The string to insert.
a - The attributes to be associated with the inserted string.
Throws:
BadLocationException - The given insert position is not valid.
Since:
v5r1m0

getMaxLength

public int getMaxLength()
Returns the maximum length allowed by this document.

Returns:
The maximum length allowed as input.
Since:
v5r1m0

setMaxLength

public void setMaxLength(int maxLength)
Sets the maximum length allowed by this document.

Parameters:
maxLength - The maximum length of input allowed by the user. A value of 0 does not restrict input.
Since:
v5r1m0