0

I'm writing custom processKeyEvent(KeyEvent e).

First of all I want only digits to be processed, so my function looks like this:

@Override
public void processKeyEvent(KeyEvent e) {
    if (Character.isDigit(e.getKeyChar())) {
        super.processKeyEvent(e);
    }
    e.consume();
}

but standard text operations such as delete, copy/paste, backspace etc. don't work. Is there way to allow them through my if-filter in a smarter manner than defining them one by one manually?

Ecto
  • 1,125
  • 1
  • 7
  • 13
  • 2
    Are you *sure* that you don't want to be using a DocumentFilter? Your question may be an XY Problem type question in disguise and you may want to tell the details behind exactly what effect you're trying to accomplish, since the best route may be completely different than what you're currently doing. – Hovercraft Full Of Eels May 08 '20 at 18:01
  • @HovercraftFullOfEels I have custom JTextField and I'm trying to make it process only numbers, I know here are answers for this kind of "filter", but I was not satisfied with the results so I am trying to achieve it this way – Ecto May 08 '20 at 18:03
  • 1
    OK, then I know I'm right -- ***NEVER*** use a KeyListener on a Swing text component. Instead get the Document and use a DocumentFilter for what you're trying to do. – Hovercraft Full Of Eels May 08 '20 at 18:05
  • What are you trying to achieve that has made you decide that you need to make a custom `JTextField` class in order to achieve it? – Abra May 08 '20 at 18:07
  • @HovercraftFullOfEels okay I'll give it a shot, what's wrong with key listeners? – Ecto May 08 '20 at 18:07
  • @Abra as mentioned earlier, I'm trying to process only numbers. One of the ways is to override keyevent. – Ecto May 08 '20 at 18:09
  • 1
    There are ___many___ examples of how to make a `JTextField` accept only numbers, for example [Restricting JTextField input to Integers](https://stackoverflow.com/questions/11093326/restricting-jtextfield-input-to-integers) – Abra May 08 '20 at 18:13
  • @Abra, hey, that Q/A looks familiar – Hovercraft Full Of Eels May 08 '20 at 18:34
  • 1
    Regarding, *"what's wrong with key listeners"*, please look at some explanations from MadProgrammer [here](https://stackoverflow.com/a/29644408/522444) and [here](https://stackoverflow.com/a/22955127/522444) for some of the problems with using KeyListeners on JTextFields, but understand that input does not necessarily have to be from the keyboard, and use of a low-level listener, such as a KeyListener, can interfere with the functionality of the text component. – Hovercraft Full Of Eels May 08 '20 at 18:40
  • Alright, document filter works very well, although it is slightly overcomplicated to implement, even though we didn't get to an actual answer (which may not exist), learning about document filter was probably much more useful. Thanks for your participation in this discussion – Ecto May 08 '20 at 19:19
  • 1
    The actual answer is this: **don't** use a KeyListener to do this – Hovercraft Full Of Eels May 08 '20 at 19:25
  • yes, but in different context you might want to filter these keys – Ecto May 08 '20 at 19:28

0 Answers0