Alan Alan - 10 months ago 104
Java Question

How to disable arrow keys for JSliders?

I want to use my arrows keys to move something on the screen but after I click on a

to use it and I use the arrows keys, they are moving the slider instead of the object. I can’t find any method to disable this. (After clicking on a slider, the sliding arrow changes from a black outline to a blue one {my presumption indicating that the arrows keys are “locked” onto the slider.})

Answer Source

Try slider.setFocusable(false);

But for a complex application, ensuring you always have control over what the arrows do may require the KeyboardFocusManager and KeyEventDispatcher APIs. These let you divert arrow (or any) keystrokes from normal handling, so must be used carefully.