JTextField设置最大长度的小代码
2009-02-20 11:49
253 查看
textfield.setDocument(new PlainDocument() { int MAX_LENGTH = 20; public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException { if (s == null || offset < 0) { return; } for (int i = 0; i < s.length(); i++) { if (getLength() > MAX_LENGTH - 1) { break; } super.insertString(offset + i, s.substring(i, i + 1), attributeSet); } return; } });
直接插入这段代码就可以了,直接替换掉textfield的Document。默认最大长度是20,想改成什么样自己调吧。
相关文章推荐
- EditText在代码中设置最大输入长度并设置为密码输入框
- EditText Java代码设置输入框最大输入长度等基础知识,自己记录一下
- EditText在代码中设置最大输入长度并设置为密码输入框
- android-代码设置editText最大长度
- edittext设置输入类型、最大长度 在代码中
- 修改sts设置代码每行长度
- 设置MyEclipse中代码的换行长度
- 代码中设置edittext的长度
- TextArea设置MaxLength属性最大输入值的js代码
- mysql整型存储字节与最大长度设置
- mysql整型存储字节与最大长度设置
- 设置textview最大长度,超出显示省略号
- 设置格式代码换行长度
- vs code如何设置每行的代码长度
- 设置Textview最大长度,超出显示省略号
- 控制图片长度比例,及图片最大高度,最大宽度 JS代码
- Sublime Text 3 如何设置代码长度的提示竖线?
- Java 编程下 Eclipse/myeclipse 如何设置单行代码显示的最大宽度
- editText设置最大长度
- 改造jquery validate.js,设置字段最大长度后,当超过这长度不能再输入