如何限制JTextPane中的字符数用户类型(Java)

时间:2012-01-16 16:45:57

标签: java swing jtextpane

我需要在输入X后不允许输入任何字符。输入X字符后,我需要发出一声嘟嘟声。我知道在用户按下回车后如何执行此操作,但我需要在用户按Enter键之前执行此操作。我在Oracle网站上发现的方法是将DocumentSizeFilter添加到JTextPane。我不能让它在他们结束时通知用户(在他们按下回车之前它不起作用)。这是我的样本。

public class EndCycleTextAreaRenderer extends JTextPane
implements TableCellRenderer {

private final int maxNumberOfCharacters = 200;

public EndCycleTextAreaRenderer() {
    StyledDocument styledDoc = this.getStyledDocument();
    AbstractDocument doc;
    doc = (AbstractDocument)styledDoc;
    doc.setDocumentFilter(new DocumentSizeFilter(maxNumberOfCharacters ));

}

4 个答案:

答案 0 :(得分:6)

覆盖JTextPane中文档的insertString方法,以便在达到最大值后不会再插入任何字符。

例如:

JTextPane textPane = new JTextPane(new DefaultStyledDocument() {
    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        if ((getLength() + str.length()) <= maxNumberOfCharacters) {
            super.insertString(offs, str, a);
        }
        else {
            Toolkit.getDefaultToolkit().beep();
        }
    }
});

<强>更新

您可以按如下方式更改课程:

public class EndCycleTextAreaRenderer extends JTextPane implements TableCellRenderer {

    private final int maxNumberOfCharacters = 200;

    public EndCycleTextAreaRenderer() {
        setStyledDocument(new DefaultStyledDocument() {
            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if ((getLength() + str.length()) <= maxNumberOfCharacters) {
                    super.insertString(offs, str, a);
                } else {
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        });
    }
}

答案 1 :(得分:2)

这是一个示例程序,对于您来说,当您第四次进入TextPane时,它会发出哔哔声,甚至没有按 Enter 键:

import javax.swing.*;
import javax.swing.text.*;

import java.awt.Toolkit;

public class TextPaneLimit extends JFrame
{
    private JPanel panel;
    private JTextPane tpane;
    private AbstractDocument abDoc;

    public TextPaneLimit()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        panel = new JPanel();
        tpane = new JTextPane();

        Document doc = tpane.getStyledDocument();
        if (doc instanceof AbstractDocument) 
        {    
            abDoc = (AbstractDocument)doc;
            abDoc.setDocumentFilter(new DocumentSizeFilter(3));
        }

        panel.add(tpane);

        add(panel);
        pack();
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new TextPaneLimit().setVisible(true);
                }
            });
    }
}

class DocumentSizeFilter extends DocumentFilter {

   private int max_Characters;
   private boolean DEBUG;

   public DocumentSizeFilter(int max_Chars) {

      max_Characters = max_Chars;
      DEBUG = false;
   }

   public void insertString(FilterBypass fb
                            , int offset
                              , String str
                                , AttributeSet a) 
   throws BadLocationException {

      if (DEBUG) {

         System.out.println("In DocumentSizeFilter's insertString method");
      }

      if ((fb.getDocument().getLength() + str.length()) <= max_Characters) 
         super.insertString(fb, offset, str, a);
      else 
         Toolkit.getDefaultToolkit().beep();
   }

   public void replace(FilterBypass fb
                       , int offset, int length
                       , String str, AttributeSet a)
   throws BadLocationException {

      if (DEBUG) {
         System.out.println("In DocumentSizeFilter's replace method");
      }
      if ((fb.getDocument().getLength() + str.length()
           - length) <= max_Characters) 
         super.replace(fb, offset, length, str, a);
      else
         Toolkit.getDefaultToolkit().beep();
   }
}

希望这可能有所帮助。

答案 2 :(得分:0)

我建议通过添加keyReleasedListener来检查输入的每个键的字符数,这是我在最近的GUI中用来检查边界看似立即并在用户输入时显示错误。

以下是我在其中一个TextFields上实现它的方法:

carbonTextField.addKeyListener(new java.awt.event.KeyAdapter() 
{
        public void keyReleased(java.awt.event.KeyEvent evt) 
        {
            carbonTextFieldKeyReleased(evt);
        }
});

答案 3 :(得分:0)

检查以下代码:

txtpnDesc.addKeyListener(new KeyAdapter(){

        @Override
        public void keyTyped(KeyEvent e) {
            if(txtpnDesc.getText().length() == 30)
            {
                e.consume();
            }

        }
    });