简体   繁体   English

从jtextfield删除最后一个字符

[英]remove last character from jtextfield

I want to a JTextField to have maximum characters, Ive been trying out this code, what im trying to do is, if a user enters more then 13 characters it should erase the last character entered, I also tried with the Unicode Character (by replacing the \\b to \) but it gives the same result, this is my code: 我想让JTextField具有最大字符数,我一直在尝试这段代码,我想做的是,如果用户输入的字符数超过13个,则它应该删除最后输入的字符,我也尝试使用Unicode字符(通过替换\\ b到\\ u0008),但给出的结果相同,这是我的代码:

if(EditTxtFName.getText().length() > 10)
{
    EditTxtFName.setBackground(Color.red);
    EditTxtFName.setText(EditTxtFName.getText() + "\b");
}
else
{
    EditTxtFName.setBackground(Color.white);
}

The output of what happens is, instead of deleting the last character is adds space and continues.. Thanks in advance.. 输出的结果是,不是删除最后一个字符,而是增加了空格并继续。

Use a DocumentFilter , it is designed to allow you to filter the content before it is added to the underlying Document of the text component... 使用DocumentFilter ,它旨在让您在将内容添加到文本组件的基础Document之前对其进行过滤...

See Implementing a Document Filter for more details 有关更多详细信息,请参见实施文档过滤器

For example... 例如...

import java.awt.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class SizeFilter extends DocumentFilter {

    private int maxCharacters;    

    public SizeFilter(int maxChars) {
        maxCharacters = maxChars;
    }

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

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

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

        if ((fb.getDocument().getLength() + str.length()
                - length) <= maxCharacters)
            super.replace(fb, offs, length, str, a);
        else
            Toolkit.getDefaultToolkit().beep();
    }
}

Which could be applied using something like... 可以使用类似...

((AbstractDocument) EditTxtFName.getDocument()).setDocumentFilter(new SizeFilter(13));

Example from DocumentFilter Examples 来自DocumentFilter示例的示例

Edit 2: 编辑2:

These solutions are outdated and should not be used. 这些解决方案已过时,不应使用。 Instead, use the DocumentFilter solution posted in this thread. 而是使用此线程中发布的DocumentFilter解决方案。

Original Answer: 原始答案:

You can add a KeyListener that caps the strings length at 13 after releasing a button like so: 您可以添加一个KeyListener ,在释放按钮后将字符串长度的上限设置为13,如下所示:

textField.addKeyListener(new KeyAdapter() {
    @Override
    public void keyReleased(KeyEvent e) {
        String typed = textField.getText();
        textField.setText(typed.substring(0, Math.min(13, typed.length())));
    }
});

This removes everything after the 13th character every time you type a character into the text field. 每次您在文本字段中键入一个字符时,这都会删除第13个字符之后的所有内容。

Edit 1: 编辑1:

Another thing you could try is this . 您可以尝试的另一件事是这个 Here, the PlainDocument class is extended in such a way that it will not accept strings that, combined with the text already contained in the document, would exceed a certain length set when creating the object. 在这里, PlainDocument类以这样的方式扩展:它不会接受字符串,该字符串与文档中已经包含的文本结合在一起,在创建对象时会超过设置的特定长度。

This method is probably a little cleaner, you don't see characters "pop up" in the text field only to be removed just moments later and also it's easier to apply this method to several JTextField s. 此方法可能更简洁一些,您不会在文本字段中看到“弹出”字符,只是稍后才将其删除,而且将此方法应用于多个JTextField更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM