How do I prevent arrow key press in JTextField from scrolling JScrollPane?

后端 未结 1 943
甜味超标
甜味超标 2021-01-23 04:17

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

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 04:24

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

    0 讨论(0)
提交回复
热议问题