Alan Alan - 11 days ago 8
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

JSlider
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

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.

Comments