import javax.swing.text.PlainDocument public class JTextFieldLimit extends PlainDocument { private int limit; JTextFieldLimit(int limit) { super(); this.limit = limit; } public void insertString( int offset, String str, AttributeSet attr ) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } } }
return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr);
/** * Skip characters of input. * This method will always skip the maximum number of characters unless * the end of the file is reached. * * @param n number of characters to skip. * @return the actual number of characters skipped. */ @Override public long skip(long n){ if (this.position + n <= this.document.getLength()){ this.position += n; return n; } else { long oldPos = this.position; this.position = this.document.getLength(); return this.document.getLength() - oldPos; } }
/** * Read a single character. * * @return the character or -1 if the end of the document has been reached. */ @Override public int read(){ if (this.position < this.document.getLength()){ try { char c = this.document.getText((int)this.position, 1).charAt(0); this.position++; return c; } catch (BadLocationException e){ // Ignore IgnoreMessageException exceptionIgnored = new IgnoreMessageException(e); LOGGER.trace(exceptionIgnored, exceptionIgnored); return -1; } } else { return -1; } }
/** Returns the length of the document. @return The length of the document in characters. **/ public synchronized int getLength () { return document_.getLength (); }
/** Returns the length of the document. @return The length of the document in characters. **/ public synchronized int getLength () { return document_.getLength (); }
/** * Skip characters of input. * This method will always skip the maximum number of characters unless * the end of the file is reached. * * @param n number of characters to skip. * @return the actual number of characters skipped. */ public long skip(long n){ if (position + n <= document.getLength()){ position += n; return n; } else { long oldPos = position; position = document.getLength(); return (document.getLength() - oldPos); } }
import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class JTextFieldLimit extends PlainDocument { //Added limited textfield so you can only enter one char private static final long serialVersionUID = 3452547874756361487L; private int limit; public JTextFieldLimit(int limit) { super(); this.limit = limit; } public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException { if (str == null) return; if ((getLength() + str.length()) <= limit) { super.insertString(offset, str, attr); } } }
/** * Read a single character. * * @return the character or -1 if the end of the document has been reached. */ public int read(){ if (position < document.getLength()){ try { char c = document.getText((int)position, 1).charAt(0); position++; return c; } catch (BadLocationException x){ return -1; } } else { return -1; } }
import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.text.AttributeSet; import javax.swing.text.PlainDocument; /** * * @author cpp-qt */ public class HexDocument extends PlainDocument { private String text = ""; @Override public void insertString(int offset, String txt, AttributeSet a) { try { text = getText(0, getLength()); if ((text + txt).matches("[0-9a-fA-F]{0,7}")) { super.insertString(offset, txt, a); } } catch (Exception ex) { Logger.getLogger(HexDocument.class.getName()).log(Level.SEVERE, null, ex); } } }
/** Writes the contents of the document to the data queue.The entry replaces the current content. System and path must be set prior to calling this method. **/ public void write () { workingEventSupport_.fireStartWorking (); // Write the contents. try { dataQueue_.write (document_.getText (0, document_.getLength ())); } catch (Exception e) { errorEventSupport_.fireError (e); } workingEventSupport_.fireStopWorking (); }
/** Writes the contents of the document to the data queue.The entry replaces the current content. System and path must be set prior to calling this method. @param key The string that contains the key for this entry. **/ public void write (String key) { workingEventSupport_.fireStartWorking (); // Write the contents. try { dataQueue_.write (key, document_.getText (0, document_.getLength ())); } catch (Exception e) { errorEventSupport_.fireError (e); } workingEventSupport_.fireStopWorking (); }
document_.remove (0, document_.getLength ());
document_.remove (0, document_.getLength ());
document_.remove (0, document_.getLength ());