Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LimitDocument.java
- package edu.tup.prepaid_canteen.actions;
- import javax.swing.text.AttributeSet;
- import javax.swing.text.BadLocationException;
- import javax.swing.text.PlainDocument;
- /**
- * Limit the document's number of input characters.
- * @author Liezl Alfaro & Erieze Lagera
- */
- public class LimitDocument extends PlainDocument {
- private int limit;
- /**
- * Setup the parameter for number of input characters.
- * @param limit Possible limit of characters for a specified document or field.
- */
- public LimitDocument( int limit ) {
- super();
- setLimit(limit);
- }
- /**
- * Get the current value for limit.
- * @return The value of <i> limit </i>.
- */
- public final int getLimit() {
- return limit;
- }
- @Override
- public void insertString( int offset, String s, AttributeSet attributeSet ) throws BadLocationException {
- if ( offset < limit ) {
- super.insertString(offset,s,attributeSet);
- }
- }
- /**
- * Setting new value for input limit without getting instantiated.
- * @param newValue New value for limit.
- */
- public final void setLimit( int newValue ) {
- this.limit = newValue;
- }
- }
- // Implementing to any fields, invoking from Constructor or jTextField's customize code.
- jTextField.setDocument(new LimitDocument(n)); // Where n is the limit of the characters
Advertisement
Add Comment
Please, Sign In to add comment