Can a scrollpane be scrolled using the arrow keys on the keyboard?

Rob Edmondson

The setScrollPosition(int x,y || Point p) member of class ScrollPane allows you the set the position of your pane directly. Implement the keyListener interface with the component you intend to give focus to. Trap key hits with one of the three methods in keyListener, such as keyPressed(KeyEvent e). Use the getKeyCode() method of KeyEvent and compare the result (integer) with KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT, or KeyEvent.VK_RIGHT, then set your ScrollPane position accordingly. You can get the current position with getScrollPosition(), modify the returned Point, then invoke setScrollPosition(Point p).

There are several ways to implement this. Here is the skeleton of one.


public class ArrowDemo extends Applet implements KeyListener
{
    ScrollPane theScrollPane;

    public void init()
    {
        theScrollPane = new ScrollPane();
        add(theScrollPane);
    }

    public void start()
    {
        addKeyListener(this);   //the Applet traps key hits when it has focus.
    }

    public void stop()
    {
        removeKeyListener(this);
    }

    public void keyPressed(keyEvent e)
    {
        int Xinc = 1;   //determine scrolling speeds
        int Yinc = 1;   
        Point p = new Point(theScrollPane.getScrollPosition()); //Get the current position
        switch(e.getKeyCode())  //Determine what key was hit
        {
            case e.VK_UP:
                p.y -= Yinc;
                break;
            case e.VK_DOWN:
                p.y += Yinc;
                break;
            case e.VK_LEFT:
                p.x -= Xinc;
                break;
            case e.VK_RIGHT:
                p.x += Xinc;
                break;
        }
        theScrollPane.setScrollPosition(p); //Move the ScrollPane
    }
    
    public void keyReleased(keyEvent e) //Must be defined
    {
    }
    
    public void keyTyped(keyEvent e)    //Must be defined
    {
    }   
}

If you are deriving your own class from ScrollPane, you may want implement the KeyListener interface into the new class. This is a better solution where more than one component may gain input focus.


public class ArrowScrollPane extends ScrollPane implements KeyListener
{
    public ArrowScrollPane()
    {
        super();    //Invoke the base constructor
    }
    
    public ArrowScrollPane(int DisplayPolicy)
    {
        super(DisplayPolicy);
    }

    public void keyPressed(keyEvent e)
    {
        int Xinc = 1;   //determine scrolling speeds
        int Yinc = 1;   
        Point p = new Point(getScrollPosition());
        switch(e.getKeyCode())
        {
            case e.VK_UP:
                p.y -= Yinc;
                break;
            case e.VK_DOWN:
                p.y += Yinc;
                break;
            case e.VK_LEFT:
                p.x -= Xinc;
                break;
            case e.VK_RIGHT:
                p.x += Xinc;
                break;
        }
        setScrollPosition(p);
    }
    
    public void keyReleased(keyEvent e)
    {
    }
    
    public void keyTyped(keyEvent e)
    {
    }   
}

//Elsewhere in your program

    ArrowScrollPane theArrowScrollPane;
    theArrowScrollPane = new ArrowScrollPane();
    add(theArrowScrollPane);
    addKeyListener(theArrowScrollPane); //The new component will trap key hits when it has input focus.
    .
    .
    .
    removeKeyListener(theArrowScrollPane);

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact