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****************************************
***************************MAIN**************************************
package GUI_HW;
import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.text.Text; import javafx.stage.Stage;
public class TempConvert extends Application {
public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("tempconvert.fxml"));
Scene scene = new Scene(root, 450, 200);
stage.setTitle("Celsius/Fahrenheit Converter"); stage.setScene(scene); stage.show(); }
public static void main(String[] args) { launch(args); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started