I'm writing a code using Java Swing to press the right button when I type a number key.
But I can't find what I want through search.
This is my code and I can't understand why this isn't working.
Please help me..

```
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;

class class01 {

    public static void main(String[] args) {

        JFrame f = new JFrame("Key event test"); 
        f.setSize(230, 500);
        f.setLayout(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JLabel label = new JLabel(); 

        JButton button1 = new JButton("Coffe"); 
        button1.setSize(100, 100);
        button1.setLocation(0, 0);

        JButton button2 = new JButton("Latte");
        button2.setSize(100, 100);
        button2.setLocation(0, 100);

        JButton button3 = new JButton("Espresso");
        button3.setSize(100, 100);
        button3.setLocation(100, 100);

        JButton button4 = new JButton("Vanilla Latte");
        button4.setSize(100, 100);
        button4.setLocation(100, 0);

        f.add(button1); 
        f.add(button2);
        f.add(button3);
        f.add(button4);

        // Show message when the corresponding button is pressed.
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                button1.keyPressed(KeyEvent.VK_1);
                JOptionPane.showMessageDialog(f.getComponent(0), "Coffee selected");
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                button2.keyPressed(KeyEvent.VK_2);
                JOptionPane.showMessageDialog(f.getComponent(0), "Latte selected");
            }
        });
        button3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                button3.keyPressed(KeyEvent.VK_3);
                JOptionPane.showMessageDialog(f.getComponent(0), "Espresso selected");
            }
        });
        button4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                button4.keyPressed(KeyEvent.VK_4);
                JOptionPane.showMessageDialog(f.getComponent(0), "Vanilla Latte selected");
            }
        });
    }
}

```
['https://Stackoverflow.com/questions/53179085', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8933767/']
The code you are showing does exactly one thing: attach action listeners to your buttons..

Meaning: when you click the button, then the listener will be called. 

You need a generic keyboard listener that translates key events into calls to the appropriate button, respectively action listener instead.
When you do this:

```
button1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            button1.keyPressed(KeyEvent.VK_1);
            JOptionPane.showMessageDialog(f.getComponent(0), "Coffee selected");
        }
    });

```

You are telling `button1` what to do when somebody clicks on the button. The line with `keyPressed` should not be there (it does not compile even).

What you need to do is listen for key presses by adding a `KeyListener` to the frame like this:

```
f.addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            if( e.getKeyChar() == KeyEvent.VK_1) {
                JOptionPane.showMessageDialog(f.getComponent(0), "Coffee selected");
            }
        }
    });

```

I repeated the `showMessageDialog`, but you should extract the actual logic into a method and call that method from within the `KeyListener` on the frame and the `ActionListener` on the button.