Question: JavaFX button needs doulbe click. I understand where it is happening but unsure how to fix this. This is a temperature converter but it does

JavaFX button needs doulbe click. I understand where it is happening but unsure how to fix this. This is a temperature converter but it does not do anything until the second click. The problem is in my controller since I have this

"public void handleButtonAction(ActionEvent event) {

btnFtoC.setOnAction(new EventHandler() {"

Can someone please help clearify what I have to change in order for this to work.

*************************Controller****************************************

package GUI_HW;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.Label;

import javafx.scene.control.TextField;

public class TempConvert_Controller {

@FXML

public Button btnFtoC;

@FXML

public Button btnCtoF;

@FXML

public TextField txtText;

@FXML

private Label actiontarget;

@FXML

public void handleButtonAction(ActionEvent event) {

btnFtoC.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent e) {

String Temp = "";

int FtoC;

if ((txtText.getText().isEmpty())) {

FtoC = 0;

actiontarget.setText("Enter Temperature");

} else {

FtoC = Integer.parseInt(txtText.getText());

int celcius = (FtoC-32)*5/9;

Temp = ("Celcius: "+celcius);

actiontarget.setText(Temp);

// input 50, answer 10

}

}

});

btnCtoF.setOnAction(new EventHandler() {

@Override

public void handle(ActionEvent e) {

String Temp = "";

int CtoF;

if ((txtText.getText().isEmpty())) {

CtoF = 0;

actiontarget.setText("Enter Temperature");

} else {

CtoF = Integer.parseInt(txtText.getText());

int fahrenheit = (CtoF*9/5) + 32;

Temp = ("Fahrenheit: "+fahrenheit);

actiontarget.setText(Temp);

// input 50, answer 122

}

}

});

}

}

*************************FXML****************************************

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!