Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JavaFX button needs doulbe click. I understand where it is happening but unsure

ID: 3736767 • Letter: J

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 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<ActionEvent>() {"

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<ActionEvent>() {

           @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<ActionEvent>() {

           @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****************************************

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>

<?import javafx.scene.control.Label?>

<?import javafx.scene.control.TextField?>

<?import javafx.scene.image.Image?>

<?import javafx.scene.image.ImageView?>

<?import javafx.scene.layout.ColumnConstraints?>

<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.layout.RowConstraints?>

<?import javafx.scene.text.Font?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="202.0" prefWidth="419.0" xmlns="http://javafx.com/javafx/9.0.4" xmlns:fx="http://javafx.com/fxml/1" fx:controller="GUI_HW.TempConvert_Controller">

<columnConstraints>

<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />

<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />

<ColumnConstraints hgrow="SOMETIMES" maxWidth="146.0" minWidth="10.0" prefWidth="79.0" />

<ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="221.0" />

</columnConstraints>

<rowConstraints>

<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />

</rowConstraints>

   <children>

<Button fx:id="btnFtoC" mnemonicParsing="false" text="F -&gt; C" GridPane.rowIndex="1" />

<Button fx:id="btnCtoF" mnemonicParsing="false" text="C -&gt; F" GridPane.columnIndex="1" GridPane.rowIndex="1" />

<TextField fx:id="txtText" minWidth="-Infinity" prefHeight="27.0" prefWidth="100.0" GridPane.columnIndex="3" GridPane.rowIndex="1" />

<Label alignment="CENTER_RIGHT" text="Temp:" GridPane.columnIndex="2" GridPane.halignment="CENTER" GridPane.rowIndex="1" />

<Label alignment="TOP_LEFT" text="Celcius/Fahrenheit Converter" textFill="RED" GridPane.columnSpan="2147483647" GridPane.valignment="TOP">

   <font>

<Font name="Verdana" size="20.0" />

   </font>

</Label>

<ImageView fitHeight="45.0" fitWidth="32.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="3" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">

   <image>

<Image url="@thermometer.png" />

   </image>

</ImageView>

<Label fx:id="actiontarget" text=" " textFill="#be00ff" GridPane.columnIndex="1" GridPane.columnSpan="2" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">

   <font>

<Font name="Verdana Bold" size="13.0" />

   </font></Label>

   </children>

</GridPane>

***************************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);
   }

Explanation / Answer

Your handleButtonAction method should be like below:

Please try and i am sure it will work in one click. Thanks!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote