user3437711
user3437711

Reputation: 25

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 \u0008) but it gives the same result, this is my code:

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..

Upvotes: 0

Views: 2287

Answers (2)

Marv
Marv

Reputation: 3557

Edit 2:

These solutions are outdated and should not be used. Instead, use the DocumentFilter solution posted in this thread.

Original Answer:

You can add a KeyListener that caps the strings length at 13 after releasing a button like so:

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.

Edit 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.

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 JTextFields.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

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...

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

Upvotes: 2

Related Questions