There is no way to directly set a JTextField‘s maximum character length. The only way for you to do that is to create your own class that inherits the PlainDocument class, instantiate an object of that class and call the JTextField method setDocument().

Below is the source for the custom made MaxLengthTextDocument class.

Having a TextField accept only numeric values can be done easily by modifying your own PlainDocument class and setting an instance of it in the TextField‘s setDocument() method. Rather than creating listeners to the TextField itself, using an inherited Document class requires you to use only one line of code.

To use the class, do this:

I am guessing this is quite a common problem that programmers encounter when they have JTextField objects inside a JTable. When you click on a cell, it would not select all the text in it. You have to double click it first. Even the DefaultCellEditor’s setClickCountToStart(1) method does not help. The workaround for this is to override the shouldSelectCell() method.

Related Posts Plugin for WordPress, Blogger...