user2076400
user2076400

Reputation: 27

how to filter certain characters in JTextField

How to prevent user from entering certain charcters in 'JTextField' and if enters that character is entered ,do not show it in the textfield

Upvotes: 2

Views: 5855

Answers (2)

camickr
camickr

Reputation: 324197

You can either use a JFormattedTextField or create a custom DocumentFilter.

Upvotes: 7

Anuj Balan
Anuj Balan

Reputation: 7729

JTextField textField = new JTextField(10);
textField.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
  char c = e.getKeyChar();
  if (//Write your condition here) {
     e.consume();  // ignore event
}});

More on the same here

Upvotes: -2

Related Questions