0
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
int x=evt.getKeyCode();
if(x>=96&&x<=105)
  {
      evt.setKeyCode(8);//Here 8 is used for Backspace key to remove the numeric character entered
  }

Int This code i want the user not to type any numeric value in jTextField but if he does so then i m trying to remove it off during runtime only.... I wrote this code but its not working as i expected it to be... Plzz Help me!!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    Don't ask the same question in two different ways; merge this question with your first one. – Dave Sep 05 '12 at 16:47
  • 1
    Possible duplicate of [*Force user not to enter numeric entry in JTextField in Java*](http://stackoverflow.com/questions/12285667/force-user-not-to-enter-numeric-entry-in-jtextfield-in-java). – trashgod Sep 05 '12 at 17:32

2 Answers2

2

You really should avoid KeyListeners, they are too limiting for what you are ultimately trying to achieve and you're only going to end up with a mutation exception as you try and change the fields document while the field is trying to change the document.

You really should be using a DocumentFilter, that's what it's design for.

((AbstractDocument)field.getDocument()).setDocumentFilter(new DocumentFilter() {

    @Override
    public void insertString(FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {

        StringBuilder sb = new StringBuilder(64);
        for (char c : text.toCharArray()) {

            if (Character.isDigit(c)) {

                sb.append(c);

            }

        }

        fb.insertString(offset, text, attr);

    }

    @Override
    public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {

        StringBuilder sb = new StringBuilder(64);
        for (char c : text.toCharArray()) {

            if (Character.isDigit(c)) {

                sb.append(c);

            }

        }

        fb.replace(offset, length, sb.toString(), attrs);

    }

});

This is a really basic example, there are plenty on SO.

Apart from avoiding mutation exceptions, the filter intercepts the update before it reaches the document/field, so the incoming changes won't be visible of the screen, you also capture any paste events or setText calls.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

While perhaps not the very best way to do it, here is one very simple way.

You can override the paintComponent() method of the JTextField like this:

JTextField textField = new JTextField(text) {

@Override
protected void paintComponent(Graphics grphcs) {
    super.paintComponent(grphcs);
    String newStr = "";
    for (char c : getText().toCharArray()) {
        if (!Character.isDigit(c)) {
            newStr += c;
        }
    }
    setText(newStr);

    }
};
Redandwhite
  • 2,529
  • 4
  • 25
  • 43
  • Dear loed no, NEVER, NEVER, NEVER, change a UI component any in ANY paint method, you'll end up consuming te CPU with repeated repaint requests – MadProgrammer Sep 05 '12 at 19:27