JTextField: How to limit the number of characters?

前端 未结 2 1459
时光说笑
时光说笑 2021-01-12 18:01

Please have a look at the following code.

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.text.AbstractDocum         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-12 18:35

    simply change your current remove method:

     @Override  
     public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException 
     {  
    
         fb.insertString(offset, "", null);
     } 
    

    for this one:

     @Override  
     public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException 
     {  
         fb.remove(offset, length);
     }
    

    it should now work.

提交回复
热议问题