0

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

Community
  • 1
  • 1
Ingen
  • 25
  • 4

1 Answers1

1

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

RAEC
  • 332
  • 1
  • 8
  • Of course, it makes perfect sense! I don't know why I didn't think about it xD Sorry for the dumb question and thank you! – Ingen Aug 06 '15 at 19:45
  • (1-), @Ingen, You shouls NOT use a KeyListener. A KeyListener whas used with AWT. Swing has better API's to use. You can add an ActionListener to the JTextField to handle the Enter key. This is the way the UI was designed to be used. Don't reinvent the wheel. For other KeyStrokes you should use KeyBindings not a KeyListener. – camickr Aug 07 '15 at 02:46
  • i havent used java in a while, have not seen KeyBindings, so if they are better than KeyListeners in any way, sure thing go right ahead. As of using ActionListeners, i would not replace one with the other. Using Action Listener to determine if it was a KEY that was pressed and then taking an action is re-inventing the wheel, that would be writing your own KeyListener when you can simply use keyListener. Again, KeyBinding might be better, i have not used it so cant tell. – RAEC Aug 08 '15 at 12:40