JTextField input constraint

Hi, I am trying to use the setDocument method to limit the number of characters that the user can enter into a textbox. But somehow this doesn't limit the number of input characters. Here's the code
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class JTextFieldCharLimit extends PlainDocument {


    private int limit;

    public JTextFieldCharLimit(int limit)
    {
        super();
        this.limit = limit;
    }
    public void inserString(int offset, String str, AttributeSet set) throws BadLocationException
    {
        if(str == null)
        {
            return;
        } else if((getLength() + str.length()) <= limit)
        {
            str = str.toUpperCase();
            super.insertString(offset, str, set);
        }
    }




}

      

I am using this class in another class where I have declared my textbox like this:

void playerInfoScreen(JFrame mainFrame, JPanel menuPanel)
    {
        final ScreenConstructor playerName = new ScreenConstructor();
        final JFrame frame = mainFrame;
        final JPanel returnPanel = menuPanel;

        final JPanel panel = playerName.createPanel("menu panel");

        final JButton returnButton = playerName.createButton("MAIN MENU");
        final JTextField textEntry = playerName.createTextField(10);
                // text field length needs to be set to prevent long texts
        final JLabel label = playerName.createLabel("Enter Player Name:");

        playerName.addButtonToPanel(panel, returnButton);
        playerName.addLabelToPanel(panel, label);
        playerName.addJTextFieldToPanel(panel, textEntry);

        textEntry.setDocument(new JTextFieldCharLimit(5));
        playerName.displayScreen(frame, panel);
            // check for esc button to let user return back to main menu




        textEntry.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                String player = textEntry.getText(); // save entered player name
                storedPlayerName = player; // store player in order to use it in highscores and display on game screen
                GameScreen game = new GameScreen(frame, panel); // go to game screen 
            }
        });


        returnButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                frame.setContentPane(returnPanel); // go back to previous panel
            }
        });




    }

      

+3


source to share


1 answer


Use DocumentFilter

. See Injecting a Document Filter and DocumentFilter Examples for details .

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();
    }
}

      



MDP loan

((AbstractDocument)textEntry.getDocument()).setDocumentFilter(new SizeFilter(5));

      

+2


source







All Articles