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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class twoButton extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setTitle("JvaFX Button"); Group root = new Group(); Scene scene = new Scene(root, 400, 300); Button btn = new Button(); btn.setText("Click Me"); btn.setLayoutY(100); btn.setLayoutX(100); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World"); } }); root.getChildren().add(btn); stage.setScene(scene); stage.show(); } } |
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.
1 |
Group root = new Group(); |
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.
1 |
Scene scene = new Scene(root, 400, 300, Color.RED); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Button btn = new Button(); btn.setText("Click Me"); btn.setLayoutY(100); btn.setLayoutX(100); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World"); } }); |
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.
1 |
root.getChildren().add(btn); |
At the end you need to set your scene object to the stage of the window, and show the window.
1 2 |
stage.setScene(scene); stage.show(); |
If you run the complete code this will be the result.
Subscribe and Get Free Video Courses & Articles in your Email