如何防止JTextField中的箭头键按下滚动JScrollPane?

时间:2013-09-06 07:45:39

标签: java swing jscrollpane jtextfield key-bindings

我想为JTextField添加自定义箭头键侦听器。但看起来箭头键绑定到我放置文本字段的JScrollPane。我该如何解开它们?

1 个答案:

答案 0 :(得分:6)

您可以尝试替换滚动窗格上的key bindings,但是可以让它们允许用户在未在文本字段中聚焦时滚动窗格。

相反,您可以将键绑定添加到不执行任何操作的文本字段,这将消耗该事件并阻止它们开始发送到滚动窗格,例如....

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPaneKeyBinding {

    public static void main(String[] args) {
        new TestScrollPaneKeyBinding();
    }

    public TestScrollPaneKeyBinding() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(new TestPane()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements Scrollable {

        public TestPane() {
            JTextField field = new JTextField(20);
            InputMap im = field.getInputMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Arrow.up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Arrow.down");
            AbstractAction doNothing = new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // doing nothing
                }
            };
            ActionMap am = field.getActionMap();
            am.put("Arrow.up", doNothing);
            am.put("Arrow.down", doNothing);
            add(field);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(100, 100);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }
    }

}