It sounds like the buttons aren't properly connected to the handler or the TextField update logic isn't functioning as expected. Let's make sure everything is properly set up step-by-step:
1. Verify FXML Setup
Ensure that all buttons and the TextField have the correct fx:id and onAction attributes set in your FXML.
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="your.package.CalculatorController">
<children>
<TextField fx:id="display" />
<Button fx:id="btn0" text="0" onAction="#handleButtonClick" />
<Button fx:id="btn1" text="1" onAction="#handleButtonClick" />
<!-- Add other buttons similarly -->
<Button text="+" onAction="#handleButtonClick"/>
<Button text="=" onAction="#handleButtonClick"/>
<Button text="C" onAction="#handleButtonClick"/>
</children>
</AnchorPane>
Ensure the fx:id and onAction attributes for each button are correctly set.
2. Verify Controller Implementation
Ensure your controller class is correctly defined and that it includes all necessary buttons and fields, and methods for handling button clicks.
package your.package
import scalafxml.core.macros.sfxml
import javafx.event.ActionEvent
import javafx.scene.control.{Button, TextField}
@sfxml
class CalculatorController(
private val display: TextField,
private val btn0: Button,
private val btn1: Button,
// Add other buttons similarly
) {
private var currentOperation: Option[String] = None
private var operand1: String = ""
private var operand2: String = ""
def handleButtonClick(event: ActionEvent): Unit = {
val button = event.getSource.asInstanceOf[Button]
val buttonText = button.getText
// Define logic for operators
if (buttonText.matches("\\d")) { // Corrected escape character
// If the buttonText is a digit, update the display and possibly operand
display.text.value += buttonText
} else if (buttonText == "+" || buttonText == "-" || buttonText == "*" || buttonText == "/") {
currentOperation = Some(buttonText)
operand1 = display.text.value
display.text.value = ""
} else if (buttonText == "=") {
operand2 = display.text.value
val result = calculateResult(operand1.toDouble, operand2.toDouble, currentOperation.getOrElse(""))
display.text.value = result.toString
} else if (buttonText == "C") {
// Clear display
display.text.value = ""
operand1 = ""
operand2 = ""
currentOperation = None
}
}
private def calculateResult(op1: Double, op2: Double, operation: String): Double = {
operation match {
case "+" => op1 + op2
case "-" => op1 - op2
case "*" => op1 * op2
case "/" => op1 / op2
case _ => 0.0 // In case of an unknown operation
}
}
}
Here, make sure that you have correctly imported the necessary JavaFX classes and that your handleButtonClick method is set up properly.
3. Ensure Main Application Loads Correct FXML
Ensure your main application loads the FXML file correctly:
package your.package
import scalafx.application.JFXApp3
import scalafx.scene.Scene
import scalafxml.core.{FXMLView, NoDependencyResolver}
object CalculatorApp extends JFXApp3 {
override def start(): Unit = {
val resource = getClass.getResource("/calculator.fxml")
val root = FXMLView(resource, NoDependencyResolver)
stage = new JFXApp3.PrimaryStage() {
title.value = "ScalaFX Calculator"
scene = new Scene(root)
resizable = false
}
}
}
4. Debugging and Logging
If it’s not working, try adding some debug prints inside your handler to see if the event is being triggered correctly.
def handleButtonClick(event: ActionEvent): Unit = {
val button = event.getSource.asInstanceOf[Button]
val buttonText = button.getText
println(s"Button clicked: $buttonText") // Debug print
// Define logic for operators
if (buttonText.matches("\\d")) {
println(s"Digit detected: $buttonText") // Debug print
// If the buttonText is a digit, update the display and possibly operand
display.text.value += buttonText
} else if (buttonText == "+" || buttonText == "-" || buttonText == "*" || buttonText == "/") {
println(s"Operator detected: $buttonText") // Debug print
currentOperation = Some(buttonText)
operand1 = display.text.value
display.text.value = ""
} else if (buttonText == "=") {
println(s"Equality detected") // Debug print
operand2 = display.text.value
val result = calculateResult(operand1.toDouble, operand2.toDouble, currentOperation.getOrElse(""))
display.text.value = result.toString
} else if (buttonText == "C") {
println(s"Clear detected") // Debug print
// Clear display
display.text.value = ""
operand1 = ""
operand2 = ""
currentOperation = None
}
}
Run your application and watch the console output to see if the events are firing and if the logic inside is working as expected. If you see the debug prints, but the TextField still isn’t updating, there might be a deeper issue in your environment set up or how the components are linked.
By systematically verifying each of these points, you should be able to ensure that your calculator functions as expected and updates the TextField appropriately when buttons are clicked.