Search code examples
javaswingjpanelpaintcomponentkeylistener

KeyListener not working with PaintComponent


My program is supposed to wait for either the left or right arrow key to be pressed, then change a value so the next time PaintComponent is updated, the screen looks different. However, the screen doesn't change when I run the program.

Here are the variable declarations:

static KeyListener listen;

public static int slide = 0;

Here's the KeyListener declaration in the main:

listen = new KeyListener() {
        public void keyPressed(KeyEvent arg0) {
            if(arg0.getKeyCode() == KeyEvent.VK_LEFT && slide>0) {
                slide--;
                System.out.println("Left pressed. Slide is " + slide);
            }
            else if(arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
                slide++;
                System.out.println("Right pressed. Slide is " + slide);
            }
        }
        public void keyReleased(KeyEvent arg0) {}
        public void keyTyped(KeyEvent arg0) {}

    };

Here is the methods used for the painting:

public void paintComponent(Graphics g){
        super.paintComponent(g);
        this.setBackground(new Color(0, 0, 20));
        g.drawImage(exitButton, 20, 20, null);
        drawBoard(g);
    }



    public void drawBoard(Graphics g){

        if(slide == 0) {
            String[] text0 = {"This is the test",
                    "for the Presenter application",
                    "Here is a nicely framed picture of the Berlin Wall."
                    };
            makeTextBox(text0);
            g.drawImage(textBox, textX, textY,(boxW)*2,(boxH)*2, null);
            g.setColor(Color.blue);
            g.fillRect(wallX-10, wallY-10, wallW+20, wallH+20);
            g.drawImage(wall, wallX, wallY, null);
        }
        else if(slide == 1) {
            String[] text1 = {"Here is the second page...",
                                "Welcome."};
            makeTextBox(text1);
            g.drawImage(textBox, textX, textY,(boxW)*2,(boxH)*2, null);
        }

    }

When I run the program, the program prints the if(slide == 0) case, but when I press the arrow keys, nothing changes on screen.


Solution

  • You need to add the key listener to the component before it will be called:

    addKeyListener(new KeyListener() {
        /* ... methods as before ... */
    });
    

    You also need to make the component focusable and give it the focus:

    setFocusable(true);
    requestFocusInWindow();
    

    Finally, if you want to redraw the screen when the key is pressed, you have to call the component's repaint() method in the key press handler.