Search code examples
swingjslider

JSlider key bindings


So I'm a bit nonplussed at the JSlider default key bindings for vertical sliders.

  • Home: goes to the bottom (why not the top?)
  • End: goes to the top (why not the bottom?)
  • PageUp/PageDown: direction good, but goes up by some amount K which I can't control
  • up/down arrow keys: direction good, but goes up by 1, and I have a high-granularity slider (0 to 65535) which I would like to have a little more intuitive control over.

Is there a way to customize this behavior?


Solution

  • You can access the keybindings via the getInputMap and getActionMap methods. For example, to switch the home/end key bindings (make Home go to the top, and end go to the bottom):

    InputMap im = yourSlider.getInputMap();
    im.put(KeyStroke.getKeyStroke("HOME"), "maxScroll");
    im.put(KeyStroke.getKeyStroke("END"), "minScroll");
    

    Here's a tutorial on key bindings.