hi

is it possible to limit the lenght of a JtextField?

thanks

Yup add a document to the jtextfield .. hmm like

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);
	      }
	    }
	 }

instanciate your JTextField and set the document to be one of these ^

JTextField mine = new JTextField();
mine.setDocument(new JTextFieldLimit(10));

or

JTextField mine = new JTextField(new JTextFieldLimit(10), "blah", 15);

can do something similar to make 'number only' fields or automatically convert everything to upper case etc etc etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.