Creating Calculator in JavaFX Java Java Projects by Rajesh Kumar Sahanee - April 5, 2017April 25, 20200 Post Views: 23,297 Hello Friends Today I am going to share, how to create Calculator using JavaFX. This is a simple calculator with simple UI where you can Add, Substract, Mutiply, Divide, and calculate Percentage. So here is the code CalculatorUI.fxml CalculatorUI.fxml XHTML <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.ColumnConstraints?> <?import javafx.scene.layout.GridPane?> <?import javafx.scene.layout.RowConstraints?> <?import javafx.scene.text.Font?> <AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="335.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zatackcoder.CalculatorUIController"> <children> <BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <top> <TextField fx:id="outputTF" alignment="CENTER_RIGHT" promptText="0"> <font> <Font size="30.0" /> </font> </TextField> </top> <center> <GridPane BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="7" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="8" GridPane.columnIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="9" GridPane.columnIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="\%" GridPane.columnIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="X" GridPane.columnIndex="3" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onCEClick" text="CE" GridPane.columnIndex="4" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="/" GridPane.columnIndex="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="=" GridPane.columnIndex="4" GridPane.rowIndex="2" GridPane.rowSpan="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="." GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onDELClick" text="DEL" GridPane.columnIndex="2" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" /> </children> </GridPane> </center> </BorderPane> </children> </AnchorPane> 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 <?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?><?import javafx.scene.control.TextField?><?import javafx.scene.layout.AnchorPane?><?import javafx.scene.layout.BorderPane?><?import javafx.scene.layout.ColumnConstraints?><?import javafx.scene.layout.GridPane?><?import javafx.scene.layout.RowConstraints?><?import javafx.scene.text.Font?> <AnchorPane id="AnchorPane" prefHeight="300.0" prefWidth="335.0" xmlns="http://javafx.com/javafx/8.0.101" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zatackcoder.CalculatorUIController"> <children> <BorderPane prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <top> <TextField fx:id="outputTF" alignment="CENTER_RIGHT" promptText="0"> <font> <Font size="30.0" /> </font> </TextField> </top> <center> <GridPane BorderPane.alignment="CENTER"> <columnConstraints> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.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 minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <children> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="7" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="8" GridPane.columnIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="9" GridPane.columnIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="\%" GridPane.columnIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="5" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="6" GridPane.columnIndex="2" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="X" GridPane.columnIndex="3" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="2" GridPane.columnIndex="1" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="3" GridPane.columnIndex="2" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="-" GridPane.columnIndex="3" GridPane.rowIndex="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onCEClick" text="CE" GridPane.columnIndex="4" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="/" GridPane.columnIndex="4" GridPane.rowIndex="1" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="=" GridPane.columnIndex="4" GridPane.rowIndex="2" GridPane.rowSpan="2" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="." GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onNumberClick" text="0" GridPane.columnIndex="1" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onDELClick" text="DEL" GridPane.columnIndex="2" GridPane.rowIndex="3" /> <Button maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" onAction="#onOperatorClick" text="+" GridPane.columnIndex="3" GridPane.rowIndex="3" /> </children> </GridPane> </center> </BorderPane> </children></AnchorPane> CalculatorUIController.java CalculatorUIController.java Java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import java.net.URL; import java.util.ResourceBundle; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Button; import javafx.scene.control.TextField; /** * FXML Controller class * * @author rajesh */ public class CalculatorUIController implements Initializable { Double temp = 0.0, sum = 0.0; boolean isOperatorPressed; String operatorPressed = ""; @FXML TextField outputTF; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { outputTF.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (!newValue.matches("\\d*([\\.]\\d*)?")) { outputTF.setText(oldValue); } } }); } @FXML private void onNumberClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if(isOperatorPressed) { outputTF.setText(btn.getText().trim()); } else { outputTF.setText(outputTF.getText().trim() + btn.getText().trim()); } isOperatorPressed = false; } } @FXML private void onOperatorClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if (!outputTF.getText().isEmpty()) { temp = Double.valueOf(outputTF.getText()); if (btn.getText().equals("%")) { temp = sum * temp / 100; } switch (operatorPressed) { case "/": sum /= temp; break; case "X": sum *= temp; break; case "+": sum += temp; break; case "-": sum -= temp; break; default: sum = temp; } } if (btn.getText().equals("=") || btn.getText().equals("%")) { outputTF.setText(String.valueOf(sum)); operatorPressed = ""; } else { outputTF.setText(""); operatorPressed = btn.getText().trim(); } isOperatorPressed = true; } } @FXML private void onDELClick(ActionEvent event) { if(outputTF.getText().length() > 0) { outputTF.setText(outputTF.getText(0, outputTF.getText().length() - 1)); } } @FXML private void onCEClick(ActionEvent event) { outputTF.setText(""); temp = 0.0; sum = 0.0; isOperatorPressed = false; operatorPressed = ""; } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package com.zatackcoder; import java.net.URL;import java.util.ResourceBundle;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.event.ActionEvent;import javafx.fxml.FXML;import javafx.fxml.Initializable;import javafx.scene.control.Button;import javafx.scene.control.TextField; /** * FXML Controller class * * @author rajesh */public class CalculatorUIController implements Initializable { Double temp = 0.0, sum = 0.0; boolean isOperatorPressed; String operatorPressed = ""; @FXML TextField outputTF; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { outputTF.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { if (!newValue.matches("\\d*([\\.]\\d*)?")) { outputTF.setText(oldValue); } } }); } @FXML private void onNumberClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if(isOperatorPressed) { outputTF.setText(btn.getText().trim()); } else { outputTF.setText(outputTF.getText().trim() + btn.getText().trim()); } isOperatorPressed = false; } } @FXML private void onOperatorClick(ActionEvent event) { if(event.getSource() instanceof Button) { Button btn = (Button)event.getSource(); if (!outputTF.getText().isEmpty()) { temp = Double.valueOf(outputTF.getText()); if (btn.getText().equals("%")) { temp = sum * temp / 100; } switch (operatorPressed) { case "/": sum /= temp; break; case "X": sum *= temp; break; case "+": sum += temp; break; case "-": sum -= temp; break; default: sum = temp; } } if (btn.getText().equals("=") || btn.getText().equals("%")) { outputTF.setText(String.valueOf(sum)); operatorPressed = ""; } else { outputTF.setText(""); operatorPressed = btn.getText().trim(); } isOperatorPressed = true; } } @FXML private void onDELClick(ActionEvent event) { if(outputTF.getText().length() > 0) { outputTF.setText(outputTF.getText(0, outputTF.getText().length() - 1)); } } @FXML private void onCEClick(ActionEvent event) { outputTF.setText(""); temp = 0.0; sum = 0.0; isOperatorPressed = false; operatorPressed = ""; }} Main.java Main.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.zatackcoder; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; /** * * @author rajesh */ public class Main extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("CalculatorUI.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } 12345678910111213141516171819202122232425262728293031323334353637 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package com.zatackcoder; import javafx.application.Application;import javafx.fxml.FXMLLoader;import javafx.scene.Parent;import javafx.scene.Scene;import javafx.stage.Stage; /** * * @author rajesh */public class Main extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("CalculatorUI.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args the command line arguments */ public static void main(String[] args) { launch(args); } } Output/Screen Shot Download NetBeans Project Calculator in JavaFX 1 file(s) 184.55 KB Download Thank you Please share