Results 1 to 3 of 3
  1. #1
    arpitvavadia is offline Member
    Join Date
    Aug 2008
    Posts
    3
    Rep Power
    0

    Thumbs down JScrollPane And ArrowKeys

    Hi !
    How can I bind arrow keys to my JScrollPane . So Instead of mouse , I can use arrow key to navigate.

    ImageIcon image = new ImageIcon ("ar.gif");
    imageLabel = new JLabel(image);
    imagePane = new JScrollPane(imageLabel);
    imagePane.setPreferredSize(new Dimension (1200, 725));

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Does the JScrollPane have methods you can call to position its view? If you can find them in the API doc, then you need to capture the keys and have them call the methods to scroll the view.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    To see which keys are already bound to JScrollBar see this.
    You could bind the page_up and page_down keys to do vertically what ctrl_home and ctrl_end keys do for the horizontal.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.swing.*;
    
    public class ArrowScrolling {
        JLabel label;
    
        private JScrollPane getContent(BufferedImage image) {
            label = new ScrollingLabel(new ImageIcon(image), JLabel.CENTER);
            label.setFocusable(true);
            return new JScrollPane(label);
        }
    
        public static void main(String[] args) throws IOException {
            File file = new File("images/redfox.jpg");
            BufferedImage image = javax.imageio.ImageIO.read(file);
            ArrowScrolling test = new ArrowScrolling();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test.getContent(image));
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.label.requestFocusInWindow();
        }
    }
    
    class ScrollingLabel extends JLabel implements Scrollable {
        public ScrollingLabel(ImageIcon icon, int horizontalAlignment) {
            super(icon, horizontalAlignment);
            registerKeys();
        }
    
        public Dimension getPreferredScrollableViewportSize() {
            return getPreferredSize();
        }
    
        public int getScrollableBlockIncrement(Rectangle visibleRect,
                                               int orientation, int direction) {
            Dimension d = ((JViewport)getParent()).getExtentSize();
            return (orientation == SwingConstants.HORIZONTAL)
                        ? d.width
                        : d.height;
        }
    
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }
    
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }
    
        public int getScrollableUnitIncrement(Rectangle visibleRect,
                                              int orientation, int direction) {
            return 50;
        }
    
        private void registerKeys() {
            int c = JComponent.WHEN_IN_FOCUSED_WINDOW;
            InputMap inputMap = getInputMap(c);
            ActionMap actionMap = getActionMap();
            int ctrl = InputEvent.CTRL_DOWN_MASK;
            KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_UP, ctrl, false);
            inputMap.put(ks, "UP");
            actionMap.put("UP", upAction);
            ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, ctrl, false);
            inputMap.put(ks, "DOWN");
            actionMap.put("DOWN", downAction);
            ks = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ctrl, false);
            inputMap.put(ks, "LEFT");
            actionMap.put("LEFT", leftAction);
            ks = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ctrl, false);
            inputMap.put(ks, "RIGHT");
            actionMap.put("RIGHT", rightAction);
        }
    
        private Action upAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JViewport viewport = (JViewport)getParent();
                Rectangle r = viewport.getViewRect();
                int inc = getScrollableBlockIncrement(r,
                    SwingConstants.VERTICAL, -1);
                Point p = r.getLocation();
                p.y = (p.y - inc >= 0) ? p.y - inc : 0;
                viewport.setViewPosition(p);
            }
        };
    
        private Action downAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JViewport viewport = (JViewport)getParent();
                Rectangle r = viewport.getViewRect();
                int inc = getScrollableBlockIncrement(r,
                    SwingConstants.VERTICAL, 1);
                Point p = r.getLocation();
                int maxY = viewport.getView().getHeight() - r.height;
                p.y = (p.y + inc <= maxY) ? p.y + inc : maxY;
                viewport.setViewPosition(p);
            }
        };
    
        private Action leftAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JViewport viewport = (JViewport)getParent();
                Rectangle r = viewport.getViewRect();
                int inc = getScrollableBlockIncrement(r,
                    SwingConstants.HORIZONTAL, -1);
                Point p = r.getLocation();
                p.x = (p.x - inc >= 0) ? p.x - inc : 0;
                viewport.setViewPosition(p);
            }
        };
    
        private Action rightAction = new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                JViewport viewport = (JViewport)getParent();
                Rectangle r = viewport.getViewRect();
                int inc = getScrollableBlockIncrement(r,
                    SwingConstants.HORIZONTAL, 1);
                Point p = r.getLocation();
                int maxX = viewport.getView().getWidth() - r.width;
                p.x = (p.x + inc <= maxX) ? p.x + inc : maxX;
                viewport.setViewPosition(p);
            }
        };
    }

Similar Threads

  1. [SOLVED] JScrollPane - HELP!
    By terox13 in forum AWT / Swing
    Replies: 8
    Last Post: 05-10-2008, 04:58 AM
  2. JScrollPane with HTML
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-14-2008, 12:32 PM
  3. jscrollpane problem
    By monkey04 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2008, 06:23 AM
  4. help with JScrollPane
    By tommy in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:58 PM
  5. JScrollPane not scrolling
    By Riftwalker in forum Advanced Java
    Replies: 2
    Last Post: 07-17-2007, 09:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •