HƯỚNG DẪN JTextField with letters only ?

Joe

Thành viên VIP
21/1/13
3,074
1,344
113
Hi

Time to show you something useful. A new member asked "Bắt sự kiện nhập chữ trong JTextField" and it seems to me that some newbies are usually not very versed with OOPL. A JTextField provides a lot of Listening methods which allow the users to implement their own requirements.

One of the requirements is: a JTextField that rejects digits. An input is made by typing a key on the Keyboard. Key? Yes Key. JTextField provides the method addKeyListener. And I show you how to extend a JTextField that accepts only letters and rejects digits using KeyListener Interface.

JLetterOnlyTextField.java accepts only letter from @ A....Za....z.
Java:
import java.awt.*;
import java.awt.event.*;
//
import javax.swing.*;
// Joe Nartca (c)
public class JLetterOnlyTextField extends JTextField {
  JLetterOnlyTextField()    {
    super();
    checkLetter();
  }
  //
  JLetterOnlyTextField(int columns) {
    super(columns);
    checkLetter();
  }
  JLetterOnlyTextField(String text) {
    super(text);
    checkLetter();
  }
  JLetterOnlyTextField(String text, int columns) {
    super(text, columns);
    checkLetter();
  }
  JLetterOnlyTextField(javax.swing.text.Document doc, String text, int columns) {
    super(doc, text, columns);
    checkLetter();
  }
  //
  private void checkLetter() {
    this.addKeyListener(new LetterListening(this));
  }
  private class LetterListening implements KeyListener {
    public LetterListening(JLetterOnlyTextField jtx) {
      this.jtx = jtx;
    }
    private JLetterOnlyTextField jtx;
    //
    public void keyTyped(KeyEvent e) { }
    public void keyPressed(KeyEvent e) { }
    //
    public void keyReleased(KeyEvent e) {
      jtx.setText(check(e));
    }
    private String check(KeyEvent e) {
      char c = e.getKeyChar();
      String txt = jtx.getText();
      if (c >= '@' && c <='z') return txt;
      return txt.replace(c+"", "");
    }
  }
}
And here is how to use JLetterOnlyTextField
Java:
import javax.swing.*;
// Joe T. Nartca (C)
public class TestJLetterOnlyTextField extends JFrame {
  //
  public static void main(String... argv) throws Exception {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    new TestJLetterOnlyTextField( );
  }
  // Constructor
  public TestJLetterOnlyTextField( ) throws Exception {
    setTitle("Test JOnlyTextField - Joe Nartca (C) -");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel msg = new JLabel("Enter text wiithout digit");
    JLetterOnlyTextField jotf = new JLetterOnlyTextField(32);
    JLabel typed = new JLabel("Try to type any digit or special character...");
    jotf.addActionListener(e -> {
      typed.setText("Your Input: "+jotf.getText());
    });
    JPanel pan = new JPanel();
    pan.add(msg); pan.add(jotf);
    JPanel south = new JPanel();
    south.add(typed);

    add("Center", pan);
    add("South", south);
    pack( );
    setVisible(true);
  }
}
Have fun...
Joe
 
Sửa lần cuối: