JavaFx Tutorial For Beginners 9 - How to build a Calculator in JavaFX Part-2

preview_player
Показать описание
In this JavaFx Tutorial For Beginners video I will show How to build a Calculator in JavaFX . This will be the part two of two part series. In addition I will cover javafx calculator tutorial, Building a Calculator with JavaFX, JavaFX begginer's simple calculator event handling, JavaFX Example Code | Calculator, Calculator in JavaFX
How to create a simple calculator with GUI in Java, javafx calculator example.

★★★Top Online Courses From ProgrammingKnowledge ★★★

★★★ Online Courses to learn ★★★

★★★ Follow ★★★

DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
Рекомендации по теме
Комментарии
Автор

A quick explanation for those who didn't get the whole thing:

Method <processNumbers>
This method is set On Action in each of the number buttons.
Boolean "start" is created to create empty string only once, because of that, you can type multiple-digit numbers.
Variable Label "result" is just an identity of calculator's Label, you can think of that as a kind of conduit between calculator's label and controller class, also, because of that, you have to always use "setText" and "getText" methods. Because Label's text is of a type String, "+" sign doesn't add up values.

Method<processOperators>
This method is set On Action in each of the operator buttons. The first is very clear - String "value" is now equal to the chosen operator. If it's not " = " it moves forward to the another if statement, it says "if operator isn't empty, stop the method here" but in fact, operator IS empty, so the code goes on. It prevents from typing more that one operator.
After this, operator is set equal to value, i.e. (chosen operator button).
Now, number1 is converted to variable of type Long, and result is again empty, to make some place for a new number (result is actually a bit confusing name for this).
From this point, this method is over, and user have to type another digit, to activate first method one more time.
Since you have both numbers, you choose equal sign button. First if statement means, that you can't have a result if you didn't choose any operator first. (to prevent from using equal sing in a beginning of an operation). The rest of the code is very easy to understand. "valueOf" makes String from float, to set it as the label text.

MatthewDHayworth
Автор

I hope you're going to do more videos on JavaFx. Great work !!!

OverNine
Автор

Its working.
I missed String Initialization.
Thank you.

siddeshhk
Автор

Very well explained, useful.

One thing, maybe making this less a simple calculator and more towards a complex one, how would you persist the value of an equation, to use that in the next. For example, using the calculator here, if I do 2 + 2, the answer is 4. Now if I click plus, the application throw an error. This is because it es expecting the number1 variable to come from the pressed key, and so as far as it is aware, me just pressing an operator, with no numbe1 via key, does not make sense.


If I were to say, at the end of the code, that number1 = result, would that always stay the same, or would it be overridden if I were to now press a number key?

ZAMINA
Автор

case "/":
return number2 == 0 ? 0 : number1 / number2;

ian_senior
Автор

operators are not working with the code what you explained in the video...
please help me to resolve it.

siddeshhk
Автор

why don't you use char data type for oprater in function instead of String !!

WaqasKhan-ysgv
Автор

Great tutorials as im brand new to Java fx....i did have a problem if you try to do another operator after getting a result I got a number format exception...presumably because its expecting a long and its a float??...either way i had to switch number 1 and 2 to floats and parse the string as a float.

Also i made the calculate method static and accessed it directly as we dont need separate objects for this up the good work though.

netfusionuk
Автор

thank you so much for your great work.

shammya
Автор

Great video, My program wasnt working at all, it showed me some path error with the processNumbers, So I changed the import Action event path to yours and it works perfectly now. just thought someone out there might run into the same issue.

angelpico
Автор

You should use type float for number1 and number2. If you use long you won't get decimals (i.e. 10/2 will result in 2.0 instead of 2.5).

KlausarMinecraft
Автор

If you get an InvocationException

go to Main.fxml file

go to the first VBox

change the above to the below one


Worked for me

muhammadjawadurrahim
Автор

Fantastic tutorial, thank you very much.

DanielMCorrea
Автор

Awesome job, your tutorials are a huge help! Thanks for taking the time to put these together.

thehiddenway
Автор

No operators are functioning, only the numbers get displayed on label.Please help

adeebqazi
Автор

My calculator can work, but it gets argument type mismatch, how so, the code is the same

devinyang
Автор

Please can anyone help me to figure out why boolean variable (start) is initialized to false into the condition (if) in process number method

Maganos
Автор

When i run it, it says Button cannot be resolved to a variable.

sf
Автор

Hi ProgrammingKnowledge, what do I have to do in order to do multiple operations? like 5 + 5 / 50 and so on? Awesome tutorial by the way!

nevermore
Автор

Operator buttons wont work for me...help please

flinthustla
join shbcf.ru