13

I've a JTextArea component inside JScrollPane and the text area is not editable. I would like to enable scrolling of the text area with up and down arrow keys (i.e. pressing the arrow keys will scroll the text area by one line). Any ideas how to achieve this?

2
  • 1
    I know you've accepted an answer, but you don't always need to write custom code. Check out my newly added suggestion.
    – camickr
    Nov 29, 2010 at 0:21
  • Yeah, by combining knowledge from both answers this kind of problems can be nicely solved. Unfortunately it's not possible to accept multiple answers ;(
    – JooMing
    Nov 29, 2010 at 7:31

5 Answers 5

15

Yes Key Bindings is the way to go, but you don't always need to create your own actions. Swing components come with default Actions that you can often reuse.

See Key Bindings for a complete list of these Actions.

Now that you know the Action name you can just bind it to a keyStroke:

JScrollBar vertical = scrollPane.getVerticalScrollBar();
InputMap im = vertical.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("DOWN"), "positiveUnitIncrement");
im.put(KeyStroke.getKeyStroke("UP"), "negativeUnitIncrement");
2
  • Thanks, this link for key bindings is very useful! The reuse of preexisting actions makes code much easier to maintain.
    – JooMing
    Nov 29, 2010 at 7:17
  • This won't work on macOs, it seems the action map is empty. More generally speaking this seems to be highly dependent of the LaF (which can be changed dynamically). The Hovercraft Full Of Eels 's solution should work in every situation because it actually registers it's own actions.
    – bric3
    Feb 6, 2022 at 15:19
7

If the JTextArea is non-editable and non-focuseable, it will not respond to the arrow keys. I'm not sure if there is a canonical way to get around this, but one way to make it respond is to set its key binding to respond to the up and down keys when the JTextArea is in the focusable window. An example of this is as follows:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.text.JTextComponent;

@SuppressWarnings("serial")
public class TestScrollingArea extends JPanel {
    private static final String UP = "Up";
    private static final String DOWN = "Down";
    private JTextArea area = new JTextArea(20, 40);
    private JScrollPane scrollPane = new JScrollPane(area);

    public TestScrollingArea() {
        // make textarea non-editable and non-focusable
        area.setEditable(false);
        area.setFocusable(false);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        add(scrollPane);

        // fill area with letters
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 100; j++) {
                area.append("abcdefg ");
            }
        }

        // have JTextArea tell us how tall a line of text is.
        int scrollableIncrement = area.getScrollableUnitIncrement(scrollPane.getVisibleRect(), 
                    SwingConstants.VERTICAL, 1);

        // add key bindings to the JTextArea 
        int condition = JTextComponent.WHEN_IN_FOCUSED_WINDOW;
        InputMap inMap = area.getInputMap(condition);
        ActionMap actMap = area.getActionMap();

        inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
        inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
        actMap.put(UP, new UpDownAction(UP, scrollPane.getVerticalScrollBar().getModel(), 
                    scrollableIncrement));
        actMap.put(DOWN, new UpDownAction(DOWN, scrollPane.getVerticalScrollBar().getModel(), 
                    scrollableIncrement));

    }

    // Action for our key binding to perform when bound event occurs
    private class UpDownAction extends AbstractAction {
        private BoundedRangeModel vScrollBarModel;
        private int scrollableIncrement;
        public UpDownAction(String name, BoundedRangeModel model, int scrollableIncrement) {
            super(name);
            this.vScrollBarModel = model;
            this.scrollableIncrement = scrollableIncrement;
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            String name = getValue(AbstractAction.NAME).toString();
            int value = vScrollBarModel.getValue();
            if (name.equals(UP)) {
                value -= scrollableIncrement;
                vScrollBarModel.setValue(value);
            } else if (name.equals(DOWN)) {
                value += scrollableIncrement;
                vScrollBarModel.setValue(value);
            }
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("TestScrollingArea");
        frame.getContentPane().add(new TestScrollingArea());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
3
  • Great! One minor problem was that pageup/pagedown keys were not working. I got it working by setting the text area to focusable and used input map for WHEN_FOCUSED instead of WHEN_IN_FOCUSED_WINDOW. Many thanks!
    – JooMing
    Nov 28, 2010 at 21:21
  • Or the page up and page down can be added as key bindings same as the above, except the increment/decrement amount will be the viewport height. Nov 28, 2010 at 21:26
  • 1
    You don't always need to create a custom Action. See my suggestion.
    – camickr
    Nov 29, 2010 at 0:16
1

Just came across this problem and while the answers was useful in driving me to the right direction some bits of the solution may have changed since then. It worked for me with he following changes: - it was the InputMap of JScrollPane instance that had to be changed - actionMapKeys had to be: "unitScrollX" and/or "scrollX" (X= Down, Up, Left, Right). They reside in BasicScrollPaneUI.

0

You should add KeyListener to your JScrollPane.

1
  • 1
    It's almost always better to use key binding than a key listener in this type of situation. Nov 28, 2010 at 19:49
0

All I had to do was to make the scroll pane request focus on mouse enter (as explained in this answer).

var scrollPane = new JScrollBar(jPanelCanvas);
scrollPane.addMouseListener(new MouseAdapter() {
    @Override
    public void mouseEntered(MouseEvent e) {
        // this seems to enable key navigation
        if ((e.getComponent() instanceof JScrollPane)) {
            e.getComponent().requestFocus();
        }
    }
});

However I'm not sure on how to tweak the actions of these keys. Maybe by tweaking the actions on the JScrollPane directly as mentioned by tinca's answer.

The call to scrollPane.getActionMap() are showing the following actions defined

  • "unitScrollRight" -> {BasicScrollPaneUI$Actions@4310}
  • "unitScrollDown" -> {BasicScrollPaneUI$Actions@4312}
  • "scrollDown" -> {BasicScrollPaneUI$Actions@4314}
  • "scrollHome" -> {BasicScrollPaneUI$Actions@4316}
  • "scrollRight" -> {BasicScrollPaneUI$Actions@4318}
  • "scrollUp" -> {BasicScrollPaneUI$Actions@4320}
  • "unitScrollLeft" -> {BasicScrollPaneUI$Actions@4322}
  • "unitScrollUp" -> {BasicScrollPaneUI$Actions@4324}
  • "scrollEnd" -> {BasicScrollPaneUI$Actions@4326}
  • "scrollLeft" -> {BasicScrollPaneUI$Actions@4328}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.