Skip to content
Advertisement

Detect when the User presses the Delete Key

(Using Java 15.0.1) I’m writing a program for school that should execute some code when the user presses the “DEL” key over a JList. I tried out some code and this is how far I got:

JavaScript

When I now press keys over that JList I get the output in the command-line. And as I want to detect the “DEL” key I tried that and it gives me this:

Pasting that in Intelliq gives me: DEL in some wired spaces this:
this That is a string so if I want to do a switch case thing to do execute code on specific keys, it tells me that I can compare a String with a Character.

How should I do that?

Advertisement

Answer

Here is a sample app that displays a JList and when you press the Delete key on the keyboard, the selected item in the JList is removed. It uses key bindings.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement