JScrollPane使用箭头键滚动

时间:2010-11-28 19:27:19

标签: java swing jscrollpane jtextarea

我在JScrollPane中有一个JTextArea组件,文本区域不可编辑。我想用上下箭头键启用文本区域的滚动(即按箭头键将文本区域滚动一行)。任何想法如何实现这一目标?

4 个答案:

答案 0 :(得分:13)

是键绑定是可行的方法,但您并不总是需要创建自己的操作。 Swing组件带有您经常可以重复使用的默认操作。

有关这些操作的完整列表,请参阅Key Bindings

现在你知道Action名称,你可以将它绑定到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");

答案 1 :(得分:6)

如果JTextArea不可编辑且不可聚焦,则它不会响应箭头键。我不确定是否有一种规范的方法来解决这个问题,但是让它响应的一种方法是设置其键绑定以在JTextArea处于可聚焦窗口时响应向上和向下键。这方面的一个例子如下:

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

答案 2 :(得分:0)

您应该将KeyListener添加到JScrollPane。

答案 3 :(得分:0)

刚刚遇到这个问题,虽然答案对我说是正确的方向,但从那时起,解决方案的某些部分可能已经发生了变化。随着他的变化,它对我有用: - 必须更改JScrollPane实例的InputMap - actionMapKeys必须是:&#34; unitScrollX&#34;和/或&#34; scrollX&#34; (X =向下,向上,向左,向右)。它们驻留在BasicScrollPaneUI中。