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?
Pressing an arrow key while SCROLL LOCK is on will scroll one row up or down or one column left or right. To use the arrow keys to move between cells, you must turn SCROLL LOCK off. To do that, press the Scroll Lock key (labeled as ScrLk) on your keyboard.
If you are facing this issue in Chrome browser, you just need to press F7 key on your keyboard, Chrome will show a confirmation box asking for your permission to turn on/off Caret Browsing feature.
What is the difference between a Scrollbar and a JScrollPane ? A Scrollbar is a Component, but not a Container. A ScrollPane is a Container. A ScrollPane handles its own events and performs its own scrolling.
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) {
}});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With