KeyListener in Textfield not firing when press enter

31,532

Solution 1

Try e.getKeyCode() instead of e.getKeyChar(). The constant KeyEvent.VK_ENTER is an int, not a char.

In other words:

if(e.getKeyCode() == KeyEvent.VK_ENTER){
      outcome = input.getText();
}

instead of

if(e.getKeyChar() == KeyEvent.VK_ENTER){
      outcome = input.getText();
}

Solution 2

First of all, you need to implement all the methods from KeyListener. You haven't implemented keyTyped and keyReleased. Another thing is you should check the key code instead of the key char because the "Enter" char is not visible, so preferably you should check if the key code equals KeyEvent.VK_ENTER. The last thing is when you hit enter you update the outcome String variable but you doesn't show it anywhere, so you need to set the text on the result JLabel. You also forgot to make the conversion. My explanation could be confusing but below is the code:

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class TempConv extends JFrame{

private JLabel info;
private JLabel result;
private JTextField input;
private String outcome;

public TempConv(){

    super("Temperature Converter");
    setLayout(new BorderLayout());

    info = new JLabel("Enter Fahrenheit Temperature");
    add(info, BorderLayout.NORTH);

    input = new JTextField(12);
    add(input, BorderLayout.CENTER);

    result  = new JLabel("Temperature in Celcius is: " + outcome);
    add(result, BorderLayout.SOUTH);

    input.addKeyListener(
            new KeyListener(){

                @Override
                public void keyPressed(KeyEvent e){

                    if(e.getKeyCode() == KeyEvent.VK_ENTER){
                        outcome = input.getText();
                        double celsius = (((Double.valueOf(outcome)) - 32) * 5 / 9 );
                        result.setText("Temperature in Celcius is: " + celsius);
                    }       
                }

                @Override
                public void keyTyped(KeyEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void keyReleased(KeyEvent e) {
                    // TODO Auto-generated method stub

                }
            }
        );
}

public static void main(String[] args) {


    TempConv ftc = new TempConv();
    ftc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ftc.setLocationRelativeTo(null);
    ftc.setSize(370, 100);
    ftc.setVisible(true);


}

}
Share:
31,532
Miral
Author by

Miral

Updated on December 12, 2021

Comments

  • Miral
    Miral over 2 years

    I'm trying to make a program that can converts fahrenheit to celcius in java. In program i have 2 Labels and 1 TextField for input. I want to make convert temperature when user types the temperature and presses Enter. To do that, i added a key listener to my textfield but it doesn't work. When i press Enter listener don't fire at all.

    And here's my code.

    public class TempConv extends JFrame{
    
    private JLabel info;
    private JLabel result;
    private JTextField input;
    private String outcome;
    
    public TempConv(){
    
        super("Temperature Converter");
        setLayout(new BorderLayout());
    
        info = new JLabel("Enter Fahrenheit Temperature");
        add(info, BorderLayout.NORTH);
    
        input = new JTextField(12);
        add(input, BorderLayout.CENTER);
    
        result  = new JLabel("Temperature in Celcius is: " + outcome);
        add(result, BorderLayout.SOUTH);
    
        input.addKeyListener(
                new KeyListener(){
    
                    public void keyPressed(KeyEvent e){
    
                        if(e.getKeyChar() == KeyEvent.VK_ENTER){
    
                            outcome = input.getText();
                        }       
                    }
                }
            );
    }
    
    public static void main(String[] args) {
    
    
        TempConv ftc = new TempConv();
        ftc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ftc.setLocationRelativeTo(null);
        ftc.setSize(370, 100);
        ftc.setVisible(true);
    
    
    }
    
    }
    

    Edit: It works with ActionListener but i need to do it with anonymous class. Without anonymous class it fires with Enter.

  • MadProgrammer
    MadProgrammer over 11 years
    Nice suggestions, but, "When i press enter listener don't fire at all" the [Enter] key is being consumed higher up the event chain, meaning that the listener is never triggered - [Enter] is begin used by the text field for something else and is begin consumed
  • Sinkingpoint
    Sinkingpoint over 11 years
    This is true, although I assume they are judging whether or not the listener fires, on whether or not the if statement actually causes an output to be printed. Perhaps I should not have made such an assumption...
  • MadProgrammer
    MadProgrammer over 11 years
    As I said, nice suggestion otherwise and one the OP should take into future consideration, just isn't going to help in this situation, sorry