Ingen
Ingen

Reputation: 25

Receive KeyEvents when JTextField is focused

I'm developing a game and I have a JFrame that receives in input the player name in a JTextField.

What I want is the possibility to close the window by either pressing a JButton or by pressing the ENTER key.

When the window opens the JTextField must have the focus (the cursor should appear in the component).

I already saw:

How do you make key bindings for a java.awt.Frame?

How do you make key binding for a JFrame no matter what JComponent is in focus?

but I have not solved the problem, there's probably something wrong in the focus management.

I tried the following code:

public class PlayerNameWindow extends JFrame implements KeyListener {

    private String playerName;
    private JLabel backgroundLabel;
    private JLabel enterNameLabel;
    private JButton confirmButton;
    private JTextField nameField;
    private Image background;

    public PlayerNameWindow() {

        initComponents();

    }

    private void initComponents() {

        backgroundLabel = new JLabel();
        enterNameLabel = new JLabel();
        confirmButton = new JButton();
        nameField = new JTextField();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setPreferredSize(new Dimension(400, 200));
        setResizable(false);
        getContentPane().setLayout(null);

        addKeyListener(this);

        enterNameLabel.setFont(new Font("Tahoma", 1, 18)); 
        enterNameLabel.setForeground(new Color(255, 255, 255));
        enterNameLabel.setText("Enter your name:");
        getContentPane().add(enterNameLabel);
        enterNameLabel.setBounds(40, 80, 160, 30);

        confirmButton.setText("Confirm");
        confirmButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent evt) {
                confirmButtonMouseClicked(evt);
           }
        });
        confirmButton.setBounds(160, 150, 90, 25);
        getContentPane().add(confirmButton);

        getContentPane().add(nameField);
        nameField.setBounds(220, 80, 140, 30);

        getContentPane().add(backgroundLabel);
        backgroundLabel.setBounds(0, 0, 400, 200);

        pack();
        setLocationRelativeTo(null); 

    }                      

    private void confirmButtonMouseClicked(MouseEvent evt) { 

        confirmAction();

    }         

    private void confirmAction() {

        playerName = nameField.getText();
        System.exit(0);

    }

    public String getPlayerName() {

          return this.playerName;

    }

    public void keyPressed(KeyEvent e) {

         int code = e.getKeyCode();
         if (code == KeyEvent.VK_ENTER)
             System.exit(0);

    }

    public void keyReleased(KeyEvent e) {

           //do-nothing

    }

    public void keyTyped(KeyEvent e) {

          //do-nothing

    }

}

How can I do?

Thanks

Upvotes: 0

Views: 228

Answers (1)

RAEC
RAEC

Reputation: 332

add keyListener to the JTextField. in your code, nameField.addKeyListener(this);

Upvotes: 1

Related Questions