0

I am trying to use KeyEvent to limit the characters in a jTextField that it can take as input on the KeyTyped event using the following code:


private void userIDFieldKeyTyped(java.awt.event.KeyEvent evt) {                                     
        char c = evt.getKeyChar();
        if(c!=KeyEvent.VK_BACK_SPACE && c!=KeyEvent.VK_DELETE){
            if (Character.isLetter(c) && Character.isDigit(c)){
            } else {
                evt.consume();
            }
        }
    }

But the above code is not working as expected, it do not take any character as input, as it is clear from the code I am trying to input only AlphaNumeric characters, is there any way to remove this problem? Also I have tried using KeyEvent.VK_ALPHANUMERIC but it didn't work either.

Edit: What I mean by limiting the characters is that the text field can take input only certain characters which I have allowed and not that to stop takinginput after a certain number of characters has been alread been entered.

Aditya
  • 126
  • 9
  • Don't use a `KeyListener` for that, use a [`DocumentListener`](https://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html) – Frakcool May 23 '19 at 16:28
  • 3
    Possible duplicate of [How To limit the number of characters in JTextField?](https://stackoverflow.com/questions/3519151/how-to-limit-the-number-of-characters-in-jtextfield) – Frakcool May 23 '19 at 16:29

1 Answers1

0

I think that JFormattedTextField handles all your requirements. It can limit the number of characters entered, as well as filter out non-required characters. Here is a small example. The MaskFormatter is responsible for handling the required limitations.

import java.awt.EventQueue;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.text.MaskFormatter;

/**
 * For testing class {@code javax.swing.JFormattedTextField}
 */
public class FmTxtFld implements Runnable {
    private JFrame frame;
    private JFormattedTextField  fmtTxtFld;

    @Override // java.lang.Runnable
    public void run() {
        try {
            showGui();
        }
        catch (ParseException xParse) {
            xParse.printStackTrace();
        }
    }

    private void showGui() throws ParseException {
        frame = new JFrame("FmTxtFld");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // Ten alphanumeric characters, i.e. letter or digit.
        MaskFormatter formatter = new MaskFormatter("AAAAAAAAAA"); //throws java.text.ParseException

        fmtTxtFld = new JFormattedTextField(formatter);
        fmtTxtFld.setColumns(10);
        JLabel label = new JLabel("JFormattedTextField");
        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(fmtTxtFld);
        frame.add(panel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        FmTxtFld instance = new FmTxtFld();
        EventQueue.invokeLater(instance);
    }
}

If you run the above code you will see that the text field only echoes valid characters, i.e. only letters and digits, and will also not accept more that 10 characters.

Abra
  • 19,142
  • 7
  • 29
  • 41