简体   繁体   English

如何在按下箭头键时阻止JScrollPane滚动

[英]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. 我在JScrollPane中有一个JPanel,而JPanel在函数中使用了箭头键。 Its annoying that the JScrollPane scrolls when the arrow keys are pressed. 令人讨厌的是,当按下箭头键时,JScrollPane会滚动。 How do i make it so that the JScrollPane doesn't scroll when the arrow keys are pressed? 如何使它在按下箭头键时不滚动JScrollPane?

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. 根据@MadProgrammer的建议,您可以替换键盘箭头的特定操作。 Use unitScrollRight and unitScrollDown action names: 使用unitScrollRightunitScrollDown操作名称:

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 我从BasicScrollPaneUI中提取的键,因此它们可以在UI之间进行更改,但这个想法应该有效

UPDATE 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 我让它使用JList和大型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 是其他输入/操作映射命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM