如何防止JTextField中的箭头键滚动JScrollPane? [英] How do I prevent arrow key press in JTextField from scrolling JScrollPane?

查看:69
本文介绍了如何防止JTextField中的箭头键滚动JScrollPane?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的JTextField添加一个自定义的箭头侦听器.但是看起来箭头键绑定到了我在其上放置文本字段的JScrollPane.如何解除绑定?

I'd like to add a custom arrow key listener for my JTextField. But looks like the arrow keys are bound to the JScrollPane on which I put my text field. How do I unbind them?

推荐答案

您可以尝试替换键绑定,但是保留它们以允许用户在未将焦点放在文本字段中时滚动窗格是很有意义的.

You could try replacing the key bindings on the scroll pane, but it might make sense to keep them to allow the user to scroll the pane when they are not focused in your text field.

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

Instead, you can add key bindings to the text field that do nothing, which will consume the event and prevent them from begin sent to the scroll pane, for example....

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

}

这篇关于如何防止JTextField中的箭头键滚动JScrollPane?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆