Home Ask Login Register

Developers Planet

Your answer is one click away!

Abbes January 2017

two textfield update each other

I try to convert data but without clicking on a button, When I enter the data in the 1st textfield nothing happens

JTextField textC = new JTextField() ;
  JTextField textF = new JTextField() ;
  labelC.setText("Celsius");
  labelF.setText("Fahrenheit");

ActionListener textFieldCListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
    String value = textC.getText();
    try {
        float valC = new Float(value);
        float valF = valC * 1.8f + 32;
        textF.setText(Float.toString(valF));
    } catch (Exception exp) {
        textF.setText("");
        textC.setText("");
    }
}};

Answers


Azodious January 2017

You should add ActionListener to your JTextField object.

textC.addActionListener(textFieldCListener);

See this: What addActionListener does?

Post Status

Asked in January 2017
Viewed 1,029 times
Voted 6
Answered 1 times

Search




Leave an answer


Quote of the day: live life