John
John

Reputation: 3807

How to prevent JScrollPane from scrolling when arrow keys are pressed

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?

Upvotes: 5

Views: 2814

Answers (2)

tenorsax
tenorsax

Reputation: 21233

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) {
    }});

Upvotes: 8

MadProgrammer
MadProgrammer

Reputation: 347334

I think you're going to have to replace the input/action map reference

ActionMap am = scrollPane.getActionMap();
am.remove("scrollDown");
am.remove("scrollUp");

The keys I extracted from the BasicScrollPaneUI so they may change between UI's but the idea should work

UPDATE

Okay, that sucked. I was hoping to get away with simple.

    InputMap im = comp.getInputMap();
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "scrollDown");

    ActionMap am = comp.getActionMap();
    am.put("scrollDown", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {

            System.out.println(e.getSource() + " - no go down");

        }
    });

Should result in the action been nullified. I got it to work with a JList and large JPanel

While I'm here:

    private static final String SCROLL_UP = "scrollUp";
    private static final String SCROLL_DOWN = "scrollDown";
    private static final String SCROLL_HOME = "scrollHome";
    private static final String SCROLL_END = "scrollEnd";
    private static final String UNIT_SCROLL_UP = "unitScrollUp";
    private static final String UNIT_SCROLL_DOWN = "unitScrollDown";
    private static final String SCROLL_LEFT = "scrollLeft";
    private static final String SCROLL_RIGHT = "scrollRight";
    private static final String UNIT_SCROLL_LEFT = "unitScrollLeft";
    private static final String UNIT_SCROLL_RIGHT = "unitScrollRight";

Are the other input/action map commands

Upvotes: 4

Related Questions