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?
Asked
Active
Viewed 2,725 times
2 Answers
7
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) {
}});

tenorsax
- 21,123
- 9
- 60
- 107
4
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

MadProgrammer
- 343,457
- 22
- 230
- 366
-
@John: That's odd; here's an [example](http://stackoverflow.com/a/7203419/230513) with which to experiment. – trashgod Jul 18 '12 at 02:20
-
What do you have inside the scroll pane?? – MadProgrammer Jul 18 '12 at 02:29
-
Someone else can run it themselves but it didn't work for me, I have a JPanel inside of the JScrollPane – John Jul 18 '12 at 03:00
-
@trashgod That's a neat idea :) – MadProgrammer Jul 18 '12 at 03:05