Abbes 5 days ago

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 5 days ago

You should add ActionListener to your JTextField object.

textC.addActionListener(textFieldCListener);

See this: What addActionListener does?

Post Status

Asked in 5 days ago
Viewed 1,029 times
Voted 6
Answered 1 times

Search




Leave an answer