Java GUI – Creating Button in JavaFX

This is our second tutorial on Java GUI Development with JavaFX, in this tutorial we want

to learn about Creating Button in JavaFX

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: Pyside2 GUI Development Tutorials 

4: Kivy GUI Development Tutorials

5: wxPython GUI Development Tutorials

 

 

 

OK first of all you need to create a new JavaFX file in your Project, you can give it what

ever name you want. for JavaFX introduction, check this article, JavaFX Window Example.

 

 

 

So now this is the complete code for Java GUI – Creating Button in JavaFX.

 

 

 

So every JavaFX applications should have a container, a container is like layout. as we

have created a Group container in the above code. but you can use different container

from JavaFX.

 

 

 

Also for every JavaFX application we need to create a Scene object. in the scene we need

to add our container with the width and height of the window, if you want to colorize your

window, you can do it in here. you can see that we have given 400 width and 300 height for the

window, also a red color for the window.

 

 

 

 

Now this is the code for Creating Button in JavaFX,  first we have created the object of

our Button class in JavaFX, after that you can see that we have set the text and also x and y

position for the button. also we have used event handler for our button, when a user click on

the button, we just print something.

 

 

 

 

When you create a widget in JavaFX, for example button,label,combobox or some other

widgets, you need to add that to your container, as we have added our this button in the

Group container.

 

 

 

 

At the end you need to set your scene object to the stage of the window, and show the window.

 

 

 

 

 

If you run the complete code this will be the result.

Java GUI - Creating Button in JavaFX
Java GUI – Creating Button in JavaFX

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×