How to prevent JScrollPane from scrolling when arrow keys are pressed

java, jscrollpane, swing

Solution

It may be too much, but you can try this:

UIManager.getDefaults().put("ScrollPane.ancestorInputMap",  
        new UIDefaults.LazyInputMap(new Object[] {}));

You could replace action globally as well:

InputMap  actionMap = (InputMap) UIManager.getDefaults().get("ScrollPane.ancestorInputMap");
actionMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {
    }});

actionMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {
    }});

Following the suggestion of @MadProgrammer you can replace particular actions for keyboard arrows. Use `unitScrollRight` and `unitScrollDown` action names:

scrollPane.getActionMap().put("unitScrollRight", new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {
    }});
scrollPane.getActionMap().put("unitScrollDown", new AbstractAction(){
    @Override
    public void actionPerformed(ActionEvent e) {
    }});

Problem

I have a JPanel inside of a JScrollPane and the JPanel uses the arrow keys in a function. Its annoying that the JScrollPane scrolls when the arrow keys are pressed. How do i make it so that the JScrollPane doesn't scroll when the arrow keys are pressed?

Original source